Created
September 17, 2016 08:00
-
-
Save groz/28c78a6c002d3917d3dd8ebb406415e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 (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) | |
elapsed-time | |
) | |
(define (search-for-primes start end) | |
(define (iter n) | |
(timed-prime-test n) | |
(cond ((< n end) (iter (+ 2 n))) | |
(else n) | |
) | |
) | |
(iter (if (even? start) (+ 1 start) start)) | |
) | |
(define (timed-test f) | |
(define (inner start-time) | |
(display (f)) | |
(newline) | |
(display (- (runtime) start-time)) | |
) | |
(inner (runtime)) | |
) | |
(define (void-find-primes) | |
(define (iter current max counter) | |
(if (< current max) | |
(iter (+ 1 current) max (if (prime? current) (+ 1 counter) counter)) | |
counter | |
) | |
) | |
(iter 2 (* 1000 200) 0) | |
) | |
(timed-test void-find-primes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment