Solved #21 in scala