-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises_1_2_6.scm
166 lines (137 loc) · 4.48 KB
/
exercises_1_2_6.scm
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
;;; Exercise 1.21 Find divsors for 199, 1999, 199999
(define (smallest-divisor n) (find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b) (= (remainder b a) 0))
(smallest-divisor 199)
(smallest-divisor 1999)
(smallest-divisor 199999)
;;; Exercise 1.22
;;;
;;; This exercise is to show a slowdown proportional to the big-O time
;;; discussed in the text. On my i7 processor with tons of ram using Release
;;; 9.0.1 of MIT Scheme most of these tests are far too slow to notice any
;;; difference in these orders of magnitude.
(define (prime? n)
(= n (smallest-divisor n)))
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time)
elapsed-time)
(define (search-for-primes a b)
(cond ((>= a b) a)
((even? a)
(timed-prime-test (+ a 1))
(search-for-primes (+ a 3) b))
(else
(timed-prime-test a)
(search-for-primes (+ a 2) b))))
(search-for-primes 1001 1100)
(search-for-primes 10001 10100)
(search-for-primes 100001 100100)
(search-for-primes 1000001 1000100)
;;; Exercise 1.23
;;; refine procedure so that it runs much faster than original one due to fact
;;; that after we've checked 2 we no longer need to check all the evens.
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(search-for-primes 1001 1100)
(search-for-primes 10001 10100)
(search-for-primes 100001 100100)
(search-for-primes 1000001 1000100)
;;; Exercise 1.24
;;;
;;; we modify the timed-prime-test to use the probalistic fermat method instead
;;; of a deterministic sieve and evalute the time of some of the primes we
;;; found in exercise 1.22
(define (expmod base exp m)
(cond ((= exp 0) 1)
((even? exp)
(remainder (square (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 n) a))
(try-it (+ 1 (random (- n 1)))))
(define (fast-prime? n times)
(cond ((= times 0) true)
((fermat-test n) (fast-prime? n (- times 1)))
(else false)))
(define (start-prime-test n start-time)
(if (fast-prime? n 10000) (report-prime (- (runtime) start-time))))
(timed-prime-test 1000003)
(timed-prime-test 1000033)
(timed-prime-test 1000037)
(timed-prime-test 1000039)
(timed-prime-test 1000081)
(timed-prime-test 1000099)
;;; Excercise 1.27
;;;
;;; Demonstrate the Carmicheal numbers 561, 1105, 1729, 2465, 2821, and 6601
;;; fool the fermat-test. Then write a procedure that takes an integer n and
;;; tests whether a^n is congruent to a mod n for all a < n and try that
;;; function on the Carmicheal numbers
(define (fermat-test n a)
(= (expmod a n n) a))
(define (fermat-full n)
(define (iter a)
(cond ((= a 1) #t)
((not (fermat-test n a)) #f)
(else (iter (- a 1)))))
(iter (- n 1)))
(fermat-full 4)
(fermat-full 561)
(fermat-full 1105)
(fermat-full 1729)
(fermat-full 2465)
(fermat-full 2821)
(fermat-full 6601)
;;; Exercise 1.28
;;;
;;; Miller-Rabin test
;;;
;;; a^(n-1) == 1 mod n if a is prime
;;; pick random a<n
;;; a^(n-1)
;;; modify expmod 1. number != 1 or n-1
;;; 2. square == 1 mod n
(define (expmod base exp m)
(cond
((= exp 0) 1)
((even? exp)
(if (and
(not (= (square (expmod base (/ exp 2) m)) (- m 1)))
(not (= (square (expmod base (/ exp 2) m)) 1))
(= (remainder (square (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 (miller-rabin n a)
(if (= (expmod a n n) 0) #t (= (expmod a n n) a)))
(define (miller-rabin-full n)
(define (iter a)
(cond ((= a 1) #t)
((not (miller-rabin n a)) #f)
(else (iter (- a 1)))))
(iter (- n 1)))
(miller-rabin-full 2)
(miller-rabin-full 3)
(miller-rabin-full 23)
(miller-rabin-full 25)
(miller-rabin-full 100)
(miller-rabin-full 6601)