Solved problem 34 in scala