; helper to return (min min+1 min+2 ... max) as a list (define (range min max) (if (> min max) null (cons min (range (+ 1 min) max)))) ; returns true if n is divisible by k (define (divides? n k) (= 0 (modulo n k))) ; a list of pairs of the form (n . #factors) ; used to cache the results of previous calls (memoize) (define cache '()) (define (count-factors n) ; if I have computed this before, return the old result (let ((memory (assoc n cache))) ; memory = (24 . 8) (if memory (cdr memory) ; else, compute result, remember result, return result (let ((result (length (filter (lambda (k) (divides? n k)) (range 1 n))))) (set! cache (cons (cons n result) cache)) result))))