Solve grade-school in scala