Solve #53 in scala