Solved problem 42 in scala