Solved problem 28 in scala