Solved #15 in scala