-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1-24.scm
68 lines (56 loc) · 1.68 KB
/
exercise1-24.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
#lang scheme
;section on primes
;fast-prime
;using Fermat's little theorem
(define (square x) (* x x))
(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 (myrandom x)
; (display (round (* x (random))))
; (newline)
(round (* x (random))))
(define (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (myrandom (- n 1)))))
(define (fast-prime? n times)
(cond ((= times 0) #t)
((fermat-test n) (fast-prime? n (- times 1)))
(else #f)))
;prime? using fast-prime
(define (prime? n)
(fast-prime? n 100))
;check order of growth through a timed test
(define (timed-prime-test n)
(newline)
(display n)
; (newline)
(start-prime-test n (current-process-milliseconds)))
(define (start-prime-test n start-time)
(cond
((prime? n) (report-prime (- (current-process-milliseconds) start-time)))))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time))
(define (search-for-primes a b)
(cond ((<= a b)
(cond ((odd? a) (timed-prime-test a)))
(search-for-primes (+ a 1) b))))
(search-for-primes 1000 1020)
(search-for-primes 10000 10200)
(search-for-primes 100000 102000)
(search-for-primes 1000000 1020000)
(newline)
(sqrt 10)
;(search-for-primes 1000000000 1000000025)
;(search-for-primes 10000000000 10000000065)
;(search-for-primes 100000000000 100000000060)
;(search-for-primes 1000000000000 1000000000065)
;results of the last 3 are 2.5s, 1.4s, 0.45s which are close to an sqrt(10) multiples...
;REVISIT for result