SICP 1.29-1.33

通用求和过程

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

下面几题答案,非本人解答,有待验证

练习1.29
函数f在范围a和b之间的定积分的近似值
(h/3)[y  0   + 4y  1   + 2y  2   + 4y  3   + 2y  4   + …… + 2y  n2   + 4y  n1   + y  n   ]
h = (b - a) / n, n为某个偶数
y  k   = f  <script id="MathJax-Element-360" type="math/tex">f</script>(a + kh) 增大n能提高近似值的精度

(define (si f a b n)
  (let ((h (/ (- b a) n)))
    (define (next x)
      (+ x 1))
    (define (yk k)
      (f (+ a (* k h))))
    (* (+ (sum yk 1 next (- n 1))
          (yk 0)
          (yk n))
       (/ h 3))))

(define (sum term a ne b)
  (if (> a b)
    0
    (+ (* (if (even? a) 2 4)
          (term a))
       (sum term (ne a) ne b))))

练习1.30

(define (si f a b n)
  (let ((h (/ (- b a) n)))
    (define (next x)
      (+ x 1))
    (define (yk k)
      (f (+ a (* k h))))
    (* (+ (sum yk 1 next (- n 1))
          (yk 0)
          (yk n))
       (/ h 3))))

(define (sum term a ne b)
  (define (iter c result)
    (if (> c b)
      result
      (iter (ne c) (+ result (* (if (even? c) 2 4) (term c))))))
  (iter a 0))

(define (cube x)
  (* x x x))

练习1.31

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

(define (factorial n)
  (define (te x)
    x)
  (define (ne x)
    (+ x 1))
  (product te 1 ne n)) 

(define (pi n)
  (define (te x)
    (/ (* (- x 1) (+ x 1))
       (* x x)))
  (define (ne x)
    (+ x 2))
  (* (product te 3 ne (+ n 2))
     4.0))

练习1.32

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

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

(define (sum term a next b)
  (define (plus x y)
    (+ x y))
  (accumulate_r plus 0 term a next b))

(define (product term a next b)
  (define (multiply x y)
    (* x y))
  (accumulate_i multiply 1 term a next b 1))

练习1.33

(define (filtered-accumulate_r combiner null-value term a next b f) 
   (if (> a b) 
     null-value 
     (if (f a) 
       (combiner (term a) 
                 (accumulate_r combiner null-value term (next a) next b f)) 
       (accumulate_r combiner null-value term (next a) next b f)))) 

(define (sum-of-squres-of-prime a b) 
   (define (combiner x y) 
     (+ x y)) 
   (define (term x) 
     (* x x)) 
   (define (next x) 
     (+ x 1)) 
   (filtered-accumulate_r combiner 0 term a next b prime?)) 


(define (product-of-primes-to-n n) 
   (define (combiner x y) 
     (* x y)) 
   (define (term x) 
     x) 
   (define (next x) 
     (+ x 1)) 
   (define (prime-to-n? x) 
     (= (gcd x n) 1)) 
   (filtered-accumulate_r combiner 1 term a next b prime-to-n?))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值