Skip to content

Instantly share code, notes, and snippets.

@DavideCanton
Created September 20, 2013 23:05
Show Gist options
  • Save DavideCanton/6645088 to your computer and use it in GitHub Desktop.
Save DavideCanton/6645088 to your computer and use it in GitHub Desktop.
Integer factorization in Chez Scheme
(define factor
(lambda (n)
(let ([next (lambda (i)
(if (zero? (modulo i 2))
(1+ i)
(+ 2 i)))])
(let f ([n n] [i 2])
(let ([d (/ n i)] [s (sqrt n)])
(cond
[(>= i s) (list n)]
[(integer? d) (cons i (f d i))]
[else (f n (next i))]))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment