Solved #20 in scala