Solved #14 in scala