Solve #62 in scala