Solved problem 35 in scala