Solved problem 62 in scala