-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-28.rkt
48 lines (40 loc) · 1.29 KB
/
1-28.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#lang racket
(define (assert condition)
(if (not condition)
(error "Condition not true")
true))
(define (assert-not condition)
(if condition
(error "Condition not true")
true))
(define (square x) (* x x))
(define (expmod base exp m)
(define (non-trivial-square-root? base exp)
(and (not (= base 1))
(= base exp)
(= (square base) (remainder 1 exp))))
(cond [(= exp 0) 1]
[(non-trivial-square-root? base exp) 0]
[(even? exp) (remainder (square (expmod base (/ exp 2) m)) m)]
[else (remainder (* base (expmod base (- exp 1) m)) m)]))
(define (miller-rabin-test n)
(define (try-it a)
(if (= a 0)
true
(and (= (expmod a (- n 1) n) 1) (try-it (- a 1)))))
(try-it (- n 1)))
(assert-not (miller-rabin-test 561))
(assert-not (miller-rabin-test 1105))
(assert-not (miller-rabin-test 1729))
(assert-not (miller-rabin-test 2465))
(assert-not (miller-rabin-test 2821))
(assert-not (miller-rabin-test 6601))
(assert (miller-rabin-test 1009))
(assert (miller-rabin-test 1013))
(assert (miller-rabin-test 1019))
(assert (miller-rabin-test 10007))
(assert (miller-rabin-test 10009))
(assert (miller-rabin-test 10037))
(assert (miller-rabin-test 100003))
(assert (miller-rabin-test 100019))
(assert (miller-rabin-test 100043))