sicp 习题 1.29 ~ 1.33

本文探讨了递归与迭代在程序设计中的应用,通过定义多个过程来实现数学计算,如求和、乘积、积分等,并对比了递归与迭代的不同实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.29
(define (inc n) (+ n 1))

(define (cube a) (* a a a))

(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))

(define (sum-cubes a b)
(sum cube a inc b))

(define (integral f a b dx)
(define (add-dx x) (+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))

(define (simpson f a b n)
(define h (/ (- b a) n))
(define (y k) (f (+ a (* k h))))
(define (next x) (+ x 1))
(define (term i)
(+ (y (- (* 2 i) 2))
(y (* 2 i))
(* 4 (y (- (* 2 i) 1)))))
(/ (* h (sum term 1 next (/ n 2))) 3))

(define (simpson1 f a b n)
(define (get-h) (/ (- b a) n))
(define (get-y k) (f (+ a (* k (get-h)))))
(define (simpson-term k)
(cond ((= k 0) (get-y k))
((= k n) (get-y k))
((= (remainder k 2) 0) (* 2.0 (get-y k)))
(else (* 4.0 (get-y k)))))
(define (simpson-next k) (+ k 1))
(* (/ (get-h) 3.0) (sum simpson-term 0 simpson-next n)))

(sum-cubes 1 10)

(integral cube 0 1 0.01)
(simpson cube 0.0 1.0 100)
(simpson1 cube 0.0 1.0 100)

(integral cube 0 1 0.001)
(simpson cube 0.0 1.0 1000)
(simpson1 cube 0.0 1.0 1000)


1.30
(define (inc n) (+ n 1))

(define (addself n) (+ (* 2 n) 1))

(define (cube a) (* a a a))

(define (f a b result)
(cond ((< a b) (f (+ a 1) b (+ a result)))
((= a b) (+ a result))))

(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))

(f 1 3 0)

(sum cube 1 inc 10)


1.31
(define (inc n) (+ n 1))

(define (cube a) (* a a a))

(define (product term a next b)
(if (> a b)
1
(* (term a)
(product term (next a) next b))))

(define (product-iter term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* result (term a)))))
(iter a 1))

(define (product-cubes a b)
(product cube a inc b))

(define (product-cubes-iter a b)
(product-iter cube a inc b))

(product-cubes 1 3)
(product-cubes-iter 1 3)

(define (double-even n)
(* 4 n n))

(define (double-odd n)
(+ (- (* 4 n n) (* 4 n)) 1))

(define (mypi n)
(* (/ (product double-even 1.0 inc n) (* (product double-odd 1.0 inc n) (* 2 n))) 2))

(mypi 80.0)


1.32
(define (inc n) (+ n 1))

(define (cube a) (* a a a))

(define (accumulate combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a)
(accumulate combiner null-value term (next a) next b))))

(define (accumulate-iter combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner result (term a)))))
(iter a null-value))

(define (sum term a next b)
(accumulate + 0 term a next b))

(define (sum-iter term a next b)
(accumulate-iter + 0 term a next b))

(define (sum-cubes a b)
(sum cube a inc b))

(define (sum-cubes-iter a b)
(sum-iter cube a inc b))

(sum-cubes 1 10)
(sum-cubes-iter 1 10)


1.33
(define (square n)
(* n n))

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

(define (plain n) n)

(define (inc n) (+ n 1))

(define (filtered-accumulate filtered combiner null-value term a next b)
(if (> a b)
null-value
(if (filtered a)
(combiner (term a) (filtered-accumulate filtered combiner null-value term (next a) next b))
(filtered-accumulate filtered combiner null-value term (+ a 1) next b))))

(define (sum term a next b)
(filtered-accumulate prime? + 0 term a next b))

(define (sum-plain a b)
(sum plain a inc b))

(sum-plain 1 10)

(define (product-prime n)
(product plain 1 inc n))

(define (product term a next b)
(define (co-prime? i)
(if (and (= (gcd i b) 1) (< i b))
#t
#f))
(filtered-accumulate co-prime? * 1 term a next b))

(product-prime 10)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值