Solved problem 25 in scala