解:相关代码如下,时间测不出来
#lang racket (define (square x) (* x x)) (define (smallest-divisor n) (define (divides? a b) (= (remainder b a) 0)) (define (next divisor) (if (= divisor 2) 3 (+ divisor 2))) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (next test-divisor))))) (find-divisor n 2)) (define (prime? n) (= n (smallest-divisor n))) (define (timed-prime-test n) (define (report-prime elapsed-time) (display "***") (display elapsed-time)) (define (start-prime-test n start-time) (cond ((prime? n) (report-prime (- (current-inexact-milliseconds) start-time))))) (newline) (display n) (start-prime-test n (current-inexact-milliseconds))) (define (search-for-primes start end num) (timed-prime-test start) (cond ((and (> num 0) (< start end)) (search-for-primes (+ start 1) end (- num 1)))))
SICP 1.21 1.22 1.23 1.24,布布扣,bubuko.com
原文:http://my.oschina.net/u/1445655/blog/303393