I'm working through the common divisibility tests, and implementing them in Racket. I'm working on divisible by three, for which I thought it'd be nice to keep summing until I got to a single digit (which I can then check for divisibility by 3).
My code is here, but it feels like there are rather a lot of steps, and I wonder if there's a way to make it feel a bit less imperative.
(define (digits n) (cond [(zero? n) '()] [else (cons (remainder n 10) (digits (quotient n 10)))])) (define (digisum n) (apply + (digits n))) (define (repeated-digit-sum x) (let ([ds (digisum x)]) (if (< ds 10) ds (repeated-digit-sum ds))))