SICP 习题1.23
来源:互联网 发布:生也有涯 知也无涯 编辑:程序博客网 时间:2024/06/13 22:33
代码有点乱,题目中要求的next我是用的next-iter,因为不知不觉中我把next这个词给用了
开始把(define (next-iter …))写在了search-for-primes后面,怎么都不对。写在前面就对了,是因为search-for-primes需要在调用之前找到next-iter,是不会向后找的。(说的不好,大致就是这意思)
(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 (next-iter test-divisor)))))(define (divides? a b) (= (remainder b a) 0))(define (prime? n) (= n (smallest-divisor n)))(define (square x) (* x x))(define (timed-prime-test n) (newline) (display n) (start-prime-test n (runtime)))(define (start-prime-test n start-time) (if (prime? n) (report-prime (- (runtime) start-time))))(define (report-prime elapsed-time) (display "***") (display elapsed-time))(define (even? x) (= (remainder x 2) 0))(define (next x) (cond ((even? x) (+ x 1)) (else x)))(define (search-for-primes now tot-num) (timed-prime-test (next now)) (cond ((= tot-num 0) #t) ((and (prime? (next now)) (= tot-num 1)) #t) ((prime? (next now)) (search-for-primes (+ (next now) 2) (- tot-num 1))) (else (search-for-primes (+ (next now) 2) tot-num))))(define (next-iter i) (cond ((= i 2) 3) (else (+ i 2))))(search-for-primes 100 3);寻找大于某个数的三个素数
0 0
- SICP 习题1.23
- SICP 习题 (1.23) 解题总结
- SICP习题
- sicp 第一章习题试做
- sicp 第一章 习题
- sicp 2.2 习题
- sicp 2.3-2.5 习题
- SICP 部分习题答案
- SICP 习题答案1.1
- SICP 习题答案1.2
- SICP 习题答案1.3
- SICP 习题答案1.4
- SICP 习题答案1.5
- SICP 习题答案1.11
- sicp习题2.1
- sicp习题2.2
- sicp 习题2.3
- sicp 习题2.5
- 关系模型之关系代数
- OpenCV学习笔记之十 (cvFillPoly&cvPolyLine)
- 基于树状数组的高效轮盘赌算法
- java网络编程_思维导图总结2
- Android-PullParser解析xml文件
- SICP 习题1.23
- scala构建工具SBT的使用_Hello_World
- scala构建工具SBT的使用_目录结构
- c#
- 深入内存模型和函数调用机制,理解析构函数的执行顺序
- java 学习问题
- scala构建工具SBT的使用_运行
- mysql 入门基础(4)—约束和视图
- Android实现多功能搜索条