Solved problem 37 in scala