-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.22.rkt
32 lines (30 loc) · 913 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
#lang planet neil/sicp
;;prime n
(define (divides? a b)
(= (remainder b a)0))
(define (square x)(* x x))
(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 (smallest-divisor n)
(find-divisor n 2))
(define(prime? n)
(= n (smallest-divisor n)))
;;display
(define (report-prime elsapsed-time)
(display "***")
(display elsapsed-time))
(define (start-prime-test n start-time)
(if(prime? n)
(report-prime (-(runtime) start-time))))
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
;;search
(define(search-for-primes from n)
(cond((= n 0)(newline) 'done)
((even? from)(search-for-primes (+ from 1) n))
((timed-prime-test from)(search-for-primes (+ from 2)(- n 1)))
(else (search-for-primes (+ from 2) n))))