Found a solution for problem 43 in scala