Solved problem 20 in scala