-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_22.scm
35 lines (28 loc) · 945 Bytes
/
1_22.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
#lang sicp
(define (search-for-primes from counter)
(if (> counter 0)
(cond ((prime? from) (and (timed-prime-test from) (search-for-primes (+ from 2) (- counter 1))))
(else (search-for-primes (+ from 1) counter)))))
(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))
(define (smallest-divisor n)
(define (sdi n c)
(cond ((> (* c c) n) n)
((= 0 (remainder n c)) c)
(else (sdi n (+ c 1)))))
(sdi n 2))
(define (prime? n)
(= n (smallest-divisor n)))
(search-for-primes 1000 3)
(search-for-primes 10000 3)
(search-for-primes 100000 3)
(search-for-primes 1000000 3)
; Yes, the algorithm takes around 3 times more time when input is doubled, as predicted