sicp练习 1.22

(define (square x)
  (* x x))

(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)
  (= (smallest-divisor n) n))

(define (search-for-primes start-num end-num)
  (search-for-primes-iter start-num end-num (runtime)))

(define (search-for-primes-iter start-num end-num start-time)
  (cond ((< start-num end-num) (begin (prime? start-num) 
                                      (search-for-primes-iter (+ start-num 1) end-num start-time)))
        ((= start-num end-num) (begin (prime? start-num) 
                                      (newline)
                                      (display "all over time: ")
                                      (display(- (runtime) start-time))
                                      (newline)
                                      (- (runtime) start-time)))))

;(search-for-primes 1000000 1000100)

;(search-for-primes 10000000 10000100)

;(search-for-primes 100000000 100000100)

(/ (search-for-primes 10000000 10001000)       (search-for-primes 1000000 1001000) 1.0)
(/ (search-for-primes 100000000 100001000)     (search-for-primes 10000000 10001000) 1.0)
(/ (search-for-primes 1000000000 1000001000)   (search-for-primes 100000000 100001000) 1.0)
(/ (search-for-primes 10000000000 10000001000) (search-for-primes 1000000000 1000001000) 1.0)
 

你可能感兴趣的:(SICP)