Solve #63 in scala