Straightforward port. Uses a modulo operator instead of repeated subtraction, because what am I, a farmer?