solution to problem 1, this time using scala and a recursive function