首页 > 代码库 > SICP 1.21 1.22 1.23 1.24
SICP 1.21 1.22 1.23 1.24
解:相关代码如下,时间测不出来
#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)))))
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。