grade-school exercise ported from ruby