Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created December 5, 2012 07:54
Show Gist options
  • Save yao2030/4213597 to your computer and use it in GitHub Desktop.
Save yao2030/4213597 to your computer and use it in GitHub Desktop.
(define (expmod base exp m)
(cond ((= exp 0) 1)
((even? exp)
(remainder (square (expmod base (/ exp 2) m)) m))
(else (remainder (* base (expmod base (- exp 1) m)) m))))
(define (fermat-every n)
(fermat-helper (- n 1) n))
(define (fermat-helper a n)
(cond ((= a 0) #t)
((= (expmod a n n) a) (fermat-helper (- a 1) n))
(else #f)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment