## SICP exercise 1.28 - Miller Rabin Primality Test

Discussion of Scheme and Racket
morbidCode
Posts: 1
Joined: Fri Aug 26, 2016 2:44 am

### SICP exercise 1.28 - Miller Rabin Primality Test

Hi, I'm new here.

Can anyone review my code? This is exercise 1.28 from SICP.

Description:

"Exercise 1.28: One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermatâ€™s Little Theorem, which states that if n is a prime number and a is any positive integer less than n, then a raised to the n-1-st power is congruent to 1 modulo n. To test the primality of a number n by the Miller-Rabin test, we pick a random number a < n and raise a to the n-1-st power modulo n using the expmod procedure. However, whenever we perform the squaring step in expmod, we check to see if we have discovered a â€œnontrivial square root of 1 modulo n,â€ that is, a number not equal to 1 or n-1 whose square is equal to 1 modulo n. It is possible to prove that if such a nontrivial square root of 1 exists, then n is not prime. It is also possible to prove that if n is an odd number that is not prime, then, for at least half the numbers a < n, computing a^n-1 in this way will reveal a nontrivial square root of 1 modulo n. (This is why the Miller-Rabin test cannot be fooled.) Modify the expmod procedure to signal if it discovers a nontrivial square root of 1, and use this to implement the Miller-Rabin test with a procedure analogous to fermat-test. Check your procedure by testing various known primes and non-primes. Hint: One convenient way to make expmod signal is to have it return 0."
https://mitpress.mit.edu/sicp/chapter1/node17.html

Here is my code:

Â  (define (square x) (* x x))
Â
Â  (define (expmod base exp m)
Â  Â  (define (expmod-iter a base exp)
Â  Â  Â  (define squareMod (remainder (square base) m))
Â  Â  Â  (cond ((= exp 0) a)
Â  Â  Â  Â  Â  Â  ((and (not (= base (- m 1)))
Â  Â  Â  Â  Â  Â  Â  Â  Â  (not (= base 1))
Â  Â  Â  Â  Â  Â  Â  Â  Â  (= squareMod 1))
Â  Â  Â  Â  Â  Â  Â 0)
Â  Â  Â  Â  Â  Â  ((even? exp)
Â  Â  Â  Â  Â  Â  Â (expmod-iter
Â  Â  Â  Â  Â  Â  Â  a
Â  Â  Â  Â  Â  Â  Â  squareMod
Â  Â  Â  Â  Â  Â  Â  (/ exp 2)))
Â  Â  Â  Â  Â  Â  (else
Â  Â  Â  Â  Â  Â  Â (expmod-iter
Â  Â  Â  Â  Â  Â  Â  (remainder (* a base) m)
Â  Â  Â  Â  Â  Â  Â  base
Â  Â  Â  Â  Â  Â  Â  (- exp 1)))))
Â  Â  (expmod-iter
Â  Â  Â 1
Â  Â  Â base
Â  Â  Â exp))

Â  (define (miller-rabin-test n)
Â  Â  (define (try-it a)
Â  Â  Â  (= (expmod a n n) 1))
Â  Â  (try-it (+ 1 (random (- n 1)))))
Â
Â  (define (fast-prime? n times)
Â  Â  (cond ((= times 0) true)
Â  Â  Â  Â  Â  ((miller-rabin-test n)
Â  Â  Â  Â  Â  Â (fast-prime? n (- times 1)))
Â  Â  Â  Â  Â  (else false)))
Â
http://paste.lisp.org/display/323708

Did I follow the exercise? Is this correct? Are there bugs? Thanks for any feeback!