-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-22.rkt
43 lines (32 loc) · 977 Bytes
/
1-22.rkt
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
#lang racket
(define (runtime) (current-inexact-milliseconds))
(define (square x) (* x x))
(define (divides? a b) (= (remainder b a) 0))
(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 (prime? n)
(= n (smallest-divisor n)))
(define (timed-prime-test n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime n (- (runtime)
start-time))
'()))
(define (report-prime n elapsed-time)
(display n)
(display " *** ")
(display elapsed-time)
(newline))
(define (search-for-primes a b)
(if (>= a b)
'()
(begin
(timed-prime-test a)
(search-for-primes (+ a 1) b))))
(search-for-primes 1000 1020)
(search-for-primes 10000 10040)
(search-for-primes 100000 100050)