;; (divides? m n) returns #t if m is a divisor of n, and #f otherwise ;; m should not be 0 (define (divides? m n) (= (modulo n m) 0)) ;; (lowest-factor n trial) returns the lowest factor of n that is ;; greater than or equal to trial. ;; trial should be at least 2. ;; (If no there is no factor less than n itself, n will be returned.) ;; Note that this is inefficient, for a number of reasons, but it is clear. (define (lowest-factor n trial) (if (divides? trial n) trial (lowest-factor n (+ 1 trial)))) ;; (factors n) returns the list of prime factors of n. ;; n should be positive. ;; Method: find the lowest factor of n, divide it out, then recurse. ;; cons is used to construct the list recursively, outside-in, ;; starting with the lowest factor (define (factors n) (if (= n 1) (list) (let ( (LF (lowest-factor n 2)) ) (cons LF (factors (/ n LF))))))