Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created December 5, 2012 07:26
Show Gist options
  • Save yao2030/4213403 to your computer and use it in GitHub Desktop.
Save yao2030/4213403 to your computer and use it in GitHub Desktop.
(define (smallest-divisor n)
(find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (divides? a b)
(= 0 (remainder b a)))
(define (next test-divisor)
(if (= test-divisor 2)
3
(+ 2 test-divisor)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment