Solved problem 27 in scala