-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1_28.rkt
42 lines (36 loc) · 1.28 KB
/
exercise1_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
#lang scheme
(define (square x)
(* x x))
#|
; use expmod many times, maybe I should use an argument to save the rusult of expmod recursion
(define (expmod base exp m)
(cond ((= exp 0) 1)
((even? exp) (if (and (= (remainder (square (expmod base (/ exp 2) m)) m) 1)
(not (= (expmod base (/ exp 2) m) 1))
(not (= (expmod base (/ exp 2) m) (- m 1))))
0
(remainder (square (expmod base (/ exp 2) m)) m)
))
(else (remainder (* base (expmod base (- exp 1) m)) m))))
|#
(define (nontrivial-square-root expmod-result m)
(if (and (= (remainder (square expmod-result) m) 1)
(not (= expmod-result 1))
(not (= expmod-result (- m 1))))
0
(remainder (square expmod-result) m)
)
)
(define (expmod base exp m)
(cond ((= exp 0) 1)
((even? exp) (nontrivial-square-root (expmod base (/ exp 2) m) m))
(else (remainder (* base (expmod base (- exp 1) m)) m))))
(define (fermat-test n)
(define (try-it a)
(= (expmod a (- n 1) n) 1))
(try-it (+ 1 (random (- n 1)))))
(define (fast-prime? n times)
(cond ((= times n) true)
((fermat-test n) (fast-prime? n (+ times 1)))
(else false)))
(fast-prime? 6601 1)