-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathex1.22-timed-prime-test.scm
62 lines (47 loc) · 1.28 KB
/
ex1.22-timed-prime-test.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
; (define (remainder a b)
; (define (iter x)
; (if (> (* x b) a)
; (- a (* (- x 1) b))
; (iter (+ x 1))))
; (iter 1)
; )
(define (smallest-divisor n)
(define (find-divisor n d)
(cond ((> (* d d) n) n)
((= (remainder n d) 0) d)
(else (find-divisor n (+ d 1)))))
(find-divisor n 2)
)
(define (prime? n)
(= (smallest-divisor n) n))
; (define (search-for-primes a b)
; (define (iter current)
; (if (prime? current)
; (write-line current))
; (if (< current b)
; (iter (+ current 2))))
; (if (even? a)
; (iter (+ a 1))
; (iter a))
; )
; (search-for-primes 10 100)
(define (search-for-primes2 a)
(define (iter current result)
(cond ((>= (length result) 3) (write-line result))
((prime? current) (iter (+ current 2) (append result (list current))))
(else (iter (+ current 2) result))))
(if (even? a)
(iter (+ a 1) (list))
(iter a (list))))
; (search-for-primes2 100)
(define (timed-search-for-primes n)
(newline)
(display n)
(start-search-for-primes n (runtime)))
(define (start-search-for-primes n start-time)
(if (search-for-primes2 n)
(report-prime (- (runtime) start-time))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time))
(timed-search-for-primes 10000)