Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(load "/Users/colin/lib/test-manager/load.scm")
(define (factor-list index product factors)
(cond ((< product 2)
'())
((= 0 (modulo product index))
(cons (cons index factors) (factor-list index (/ product index) factors)))
(else
(factor-list (+ index 1) product factors))))
(define (prime-factors product)
(map car (factor-list 2 product '())))
(define-each-test
(assert-equal (list 2) (prime-factors 2))
(assert-equal (list 3) (prime-factors 3))
(assert-equal (list 2 2) (prime-factors 4))
(assert-equal (list 5) (prime-factors 5))
(assert-equal (list 2 3) (prime-factors 6))
(assert-equal (list 7) (prime-factors 7))
(assert-equal (list 2 2 2) (prime-factors 8))
(assert-equal (list 3 3) (prime-factors 9))
(assert-equal (list 3 7 37 41) (prime-factors 31857))
)
(run-registered-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment