Blob


1 ;; (define (expmod base exp m)
2 ;; (cond ((= exp 0) 1)
3 ;; ((even? exp)
4 ;; (remainder (square (expmod base (/ exp 2) m)) m))
5 ;; (else (remainder (* base (expmod base (- exp 1) m)) m))))
7 ;; (define (fermat-test n)
8 ;; (define (try-it a)
9 ;; (= (expmod a n n) a))
10 ;; (try-it (+ 1 (random (- n 1)))))
12 ;; (define (fast-prime? n times)
13 ;; (cond ((= times 0) true)
14 ;; ((fermat-test n) (fast-prime? n (- times 1)))
15 ;; (else false)))
17 ;; (define (test-case actual expected)
18 ;; (load-option 'format)
19 ;; (newline)
20 ;; (format #t "Actual: ~A Expected: ~A" actual expected))
23 ;; Exercise 1.23. The smallest-divisor procedure shown at the start of this section does lots of needless testing: After it checks to see if the number is divisible by 2 there is no point in checking to see if it is divisible by any larger even numbers. This suggests that the values used for test-divisor should not be 2, 3, 4, 5, 6, ..., but rather 2, 3, 5, 7, 9, .... To implement this change, define a procedure next that returns 3 if its input is equal to 2 and otherwise returns its input plus 2. Modify the smallest-divisor procedure to use (next test-divisor) instead of (+ test-divisor 1). With timed-prime-test incorporating this modified version of smallest-divisor, run the test for each of the 12 primes found in exercise 1.22. Since this modification halves the number of test steps, you should expect it to run about twice as fast. Is this expectation confirmed? If not, what is the observed ratio of the speeds of the two algorithms, and how do you explain the fact that it is different from 2?
25 (define (smallest-divisor n)
26 (find-divisor n 2))
27 (define (find-divisor n test-divisor)
28 (define (next-divisor n)
29 (if (= n 2)
30 3
31 (+ n 2)))
32 (cond ((> (square test-divisor) n) n)
33 ((divides? test-divisor n) test-divisor)
34 (else (find-divisor n (next-divisor test-divisor)))))
35 (define (divides? a b)
36 (= (remainder b a) 0))
37 (define (prime? n)
38 (= n (smallest-divisor n)))
40 (define (timed-prime-test n)
41 (newline)
42 (display n)
43 (start-prime-test n (runtime)))
44 (define (start-prime-test n start-time)
45 (if (prime? n)
46 (report-prime (- (runtime) start-time))))
47 (define (report-prime elapsed-time)
48 (display " *** ")
49 (display elapsed-time))
51 (define (search-for-primes lower upper)
52 (cond ((even? lower) (search-for-primes (+ lower 1) upper))
53 ((< lower upper) (begin (timed-prime-test lower)
54 (search-for-primes (+ lower 2) upper)))
55 (else (newline)
56 (display " *** Finished *** "))))
59 (search-for-primes 100000001 100000099)
60 (search-for-primes 1000000001 1000000099)
61 (search-for-primes 10000000001 10000000099)
62 (search-for-primes 100000000001 100000000099)
63 (search-for-primes 1000000000001 1000000000099)
64 (search-for-primes 10000000000001 10000000000099)
65 (search-for-primes 100000000000001 100000000000099)
67 ;; see spreadsheet ex1-23.ods for results
68 ;; Not quite half, but close enough. This is due to introducing an extra computation at each step due to having to evaluate one extra (next-divisor test-divisor) with each call on the procedure