-
Notifications
You must be signed in to change notification settings - Fork 4
/
ex1-24-fast-search-primes.scm
76 lines (62 loc) · 2.19 KB
/
ex1-24-fast-search-primes.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
; Ex. 1.23 Search for Primes with modified test-divisor procedure
; This searches for prime numbers from a lower bound to an upper
; bound that is limited by the number of primes to be found.
(define (search-primes from how-many)
(cond ((= how-many 0)
(newline)
(display "Done"))
((even? from)
(search-primes (+ from 1) how-many))
((fast-prime? from 10) ; This feels redundant...
(timed-prime-test from)
(search-primes (+ from 2) (- how-many 1)))
(else
(search-primes (+ from 2) how-many))))
; In the procedure below, applying "runtime" returns an value
; that specifies the amount of time the system has been running.
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (fast-prime? n 10)
(report-prime (- (runtime) start-time))))
(define (report-prime elapsed-time)
(display "***")
(display (* elapsed-time 1000.0)))
; (define (prime? n)
; (= n (smallest-divisor n)))
(define (fast-prime? n times)
(cond ((= times 0) true)
((fermat-test n) (fast-prime? n (- times 1)))
(else false)))
(define (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
(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 (smallest-divisor n)
(find-divisor n 2))
; Modified this procedure to use "next" as per the question
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (divides? a b)
(= (remainder b a) 0))
(define (square n)
(* n n))
; Added next as per the question. It can be more generalised, but
; not doing that over here as it gives not extra computational
; benefit for the purpose of this question.
(define (next n)
(if (= n 2)
3
(+ n 2)))
(define (even? n)
(= (remainder n 2) 0))