Blob


1 ;; calculate base^exp modulo m
2 (define (expmod base exp m)
3 (cond ((= exp 0) 1)
4 ((even? exp)
5 (remainder (square (expmod base (/ exp 2) m)) m))
6 (else (remainder (* base (expmod base (- exp 1) m)) m))))
8 ;; tests if integer n passes fermat's little theorem
9 (define (fermat-prime? n)
10 (define (fermat-test a)
11 (cond ((= a n) #t)
12 ((not (= (expmod a n n) a)) #f)
13 (else (fermat-test (+ a 1)))))
14 (fermat-test 1))
16 (define (list-primes upper)
17 (define (test i)
18 (cond ((= i upper) (display "Finished"))
19 ((fermat-prime? i) (begin (display i)
20 (newline))))
21 (test (+ i 1)))
22 (test 2))
24 (define (test-case actual expected)
25 (load-option 'format)
26 (newline)
27 (format #t "Actual: ~A Expected: ~A" actual expected))
29 ;; (test-case (fermat-prime? 2) #t)
30 ;; (test-case (fermat-prime? 3) #t)
31 ;; (test-case (fermat-prime? 4) #f)
32 ;; (test-case (fermat-prime? 5) #t)
33 ;; (test-case (fermat-prime? 6) #f)
34 ;; (test-case (fermat-prime? 7) #t)
35 ;; (test-case (fermat-prime? 8) #f)
36 ;; (test-case (fermat-prime? 9) #f)
39 ;; (list-primes 10000)
41 ;; Carmichael Numbers
42 (test-case (fermat-prime? 561) #f)
43 (test-case (fermat-prime? 1105) #f)
44 (test-case (fermat-prime? 1729) #f)
45 (test-case (fermat-prime? 2465) #f)
46 (test-case (fermat-prime? 2821) #f)
47 (test-case (fermat-prime? 6601) #f)
50 ;; 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.
52 (expmod base exp m)
53 (= (expmod a (n-1) n) 1)
55 ;; calculate base^exp modulo m but signal if
56 (define (expmod base exp m)
57 (cond ((= exp 0) 1)
58 ((even? exp)
59 (remainder (square (expmod base (/ exp 2) m)) m))
60 (else (remainder (* base (expmod base (- exp 1) m)) m))))