;Fermat's little theorem
;If N is a prime number and A is any positive number less than N, then (A^N % N) == (A % N). (A^N stands for A raised to the Nth power.)
;This theorem can be used to test the primality of a number. If the above relation is not satisfied for a N, then N is certainly not a prime. If the above relation is satisfied for a N, then the chance is good that N is a prime. The equation is necessary for N to be a prime, but it is not sufficient.
;The programs followed are a implementation for testing the primality of a N.
;Fermat test
;n is the number to be tested
;a is the number less than n and greater than 0
(define (fermat-test n a)
(= (exponent-modulo a n n) a) )
;exponent-modulo
(define (exponent-modulo base exp mod)
(remainder (exponent base exp) mod) )
;calculate base^exp
(define (exponent base exp)
(exponent-helper base exp 1) )
;tail recursive base^exp helper
(define (exponent-helper base exp product)
(cond ( (= exp 0) product )
( (even? exp) (exponent-helper (* base base) (/ exp 2) product) )
( else (exponent-helper base (- exp 1) (* product base) ) ) ) )
;determine whether a number is even or odd
(define (even? n)
(= (remainder n 2) 0) )