Skip to content

Instantly share code, notes, and snippets.

@cky
Created June 24, 2012 21:03
Show Gist options
  • Save cky/2984979 to your computer and use it in GitHub Desktop.
Save cky/2984979 to your computer and use it in GitHub Desktop.
Modular exponentiation
(define (modexp a b q)
(define (modmul a b)
(modulo (* a b) q))
(do ((a a (modmul a a))
(b b (quotient b 2))
(r 1 (if (odd? b) (modmul r a) r)))
((zero? b) r)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment