-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.24.rkt
72 lines (57 loc) · 1.68 KB
/
1.24.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#lang sicp
(#%require (only racket random))
(define (sicp-random n)
(if (exact? n)
(random n)
(* n (random))))
(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))
(define (square x)
(* x x))
(define (fast-prime? n times)
(cond ((= times 0) true)
((fermat-test n) (fast-prime? n (- times 1)))
(else false)))
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(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 (+ (sicp-random (- n 1)))))
(define (start-prime-test n start-time)
(if (fast-prime? n 100)
(report-prime (- (runtime) start-time))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time))
(define (test1 start primesfound)
(if (= primesfound 3)
" complete "
(if (= 0 (remainder start 2))
(test1 (+ start 1) primesfound)
(cond ((fast-prime? start 10) (timed-prime-test start)
(test1 (+ start 1) (+ 1 primesfound)))
(else (test1 (+ start 1) primesfound))))))
(test1 1000 0)
(test1 10000 0)
(test1 100000 0)
(test1 1000000 0)
(test1 100000000 0)
(test1 100000000 0)
(test1 1000000000 0)