-
Notifications
You must be signed in to change notification settings - Fork 10
/
1.22.ss
52 lines (42 loc) · 1.47 KB
/
1.22.ss
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
;--------------------------------------------------------
(require (lib "19.ss" "srfi"))
(display (current-time time-process))
(define (square x) (* x x))
(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 (prime? n)
(= n (smallest-divisor n)))
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (current-time time-process)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (time-difference (current-time time-process) start-time))
#f)) ; have timed-prime-test return false
(define (report-prime elapsed-time)
(display " *** ")
(display (current-time time-process elapsed-time))
(display "s ")
(display (current-time time-process elapsed-time))
(display "ns")
#t) ; have timed-prime-test return true
(define (search-for-primes start count)
(define checked-start (if (even? start)
(+ 1 start)
start))
(define (iter now count)
(cond ((= 0 count)
(newline)
(display "***** FINISHED *****")
(newline))
((timed-prime-test now) (iter (+ 2 now) (- count 1)))
(else (iter (+ 2 now) count))))
(iter checked-start count))
(search-for-primes 10000000000 3)