Solved problem 45 in scala