SICP习题解答1.29-1.39

ex1.29

#lang racket

; exercise 1.29
(define (integ f a b n)
  (define h (/ (- b a) n))
  (define (term k)
    (define y (f (+ a (* k h))))
    (cond ((or (= k 0) (= k n)) y)
          ((even? k) (* 2 y))
          (else (* 4 y))))
  (* (/ h 3) (sum term 0 inc n)))
(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a) (sum term (next a) next b))))
(define (inc n)
  (+ n 1))
(define (cube x)
  (* x x x))
(integ cube 0 1 100) ; 1/4
(integ cube 0 1 1000); 1/4


ex1.30

#lang racket

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

ex1.31

#lang racket

; exercise 1.31
; a)
(define (product-rec term a next b)  ;; 递归版本
  (if (> a b)
      1
      (* (term a) (product-rec term (next a) next b))))

(define (factorial n)
  (product-rec identity 1 inc n))
(define (identity x) x)
(define (inc x) (+ x 1))

(define (pi-pro b)
  (define (term k)
    (* (/ (- k 1.0) k) (/ (+ k 1.0) k)))
  (define (inc2 x) (+ x 2.0))
  (* 4.0 (product-rec term 3.0 inc2 b)))
(pi-pro 1111)  ;; 3.143005557649655

; b)
(define (product-iter term a next b)  ;; 迭代版本
  (define (iter cur res)
    (if (> cur b)
        res
        (iter (next cur) (* (term cur) res))))
  (iter a 1))

ex1.32

#lang racket

; exercise 1.32
; 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 (sum term a next b) (accumulate + 0 term a next b))
(define (product term a next b) (accumulate * 1 term a next b))

; b)
(define (accumulate combiner null-value term a next b)  ;; 迭代
  (define (iter cur res)
    (if (> cur b)
        res
        (iter (next cur) (combiner res (term cur)))))
  (iter a null-value))


ex1.33

#lang racket

; exercise 1.33
(define (filtered-accumulate filter combiner null-value term a next b)
  (if (or (< a b))
      null-value
      (if (filter a) 
          (combiner (term a) (filtered-accumulate filter combiner null-value term (next a) next b))
          (combiner null-value (filtered-accumulate filter combiner null-value term (next a) next b)))))

; a)
(define (sum-of-prime a b)
  (filtered-accumulate prime? + 0 identity a inc b))
; b)
(define (f n)
  (define (relative-prime? cur)
    (= (gcd cur n) 1))
  (filtered-accumulate relative-prime? * 1 identity 1 inc n))

ex1.34-1.35

#lang racket

; exercise 1.34
;; (f f) --> (f 2) --> (2 2) 报错

; exercise 1.35
(define tolerance 0.00001)
(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))
(define (phi x)
  (fixed-point (lambda (x) (+ 1 (/ 1.0 x))) x))
(phi 1) ;; 1.6180327868852458

ex1.36

(define tolerance 0.00001)
(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (display guess)
    (newline)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))
(define (f1 x)
  (fixed-point (lambda (x) (/ (log 1000) (log x))) x))
(f1 2)

输出结果为:

2
9.965784284662087
3.004472209841214
6.279195757507157
3.759850702401539
5.215843784925895
4.182207192401397
4.8277650983445906
4.387593384662677
4.671250085763899
4.481403616895052
4.6053657460929
4.5230849678718865
4.577114682047341
4.541382480151454
4.564903245230833
4.549372679303342
4.559606491913287
4.552853875788271
4.557305529748263
4.554369064436181
4.556305311532999
4.555028263573554
4.555870396702851
4.555315001192079
4.5556812635433275
4.555439715736846
4.555599009998291
4.555493957531389
4.555563237292884
4.555517548417651
4.555547679306398
4.555527808516254
4.555540912917957
4.555532270803653

使用平均阻尼:

(define (f2 x)
  (define (average a b) (/ (+ a b) 2))
  (fixed-point (lambda (x) (average x (/ (log 1000) (log x)))) x))
(f2 2)

输出结果为:

2
5.9828921423310435
4.922168721308343
4.628224318195455
4.568346513136242
4.5577305909237005
4.555909809045131
4.555599411610624
4.5555465521473675
4.555537551999825

可见使用平均阻尼可以减少计算步数

ex1.37-1.39

#lang racket

; exercise 1.37
;; a)
(define (cont-frac n d k)  ;; 递归
  (define (f i)
    (if (= i k)
        (/ (n i) (d i))
        (/ (n i) (+ (d i) (f (+ i 1))))))
  (f 1))
(cont-frac (lambda (i) 1.0)
           (lambda (i) 1.0)
           14)  ;; k=14能达到4位精度0.6180327868852459
;; b)
(define (cont-frac-iter n d k) ;; 迭代
  (define (iter i res)
    (if (= i 0)
        res
        (iter (- i 1) (/ (n i) (+ res (d i))))))
  (iter k 0))
(cont-frac-iter (lambda (i) 1.0)
           (lambda (i) 1.0)
           14)  ;; 与cont-frac结果相同

; exercise 1.38
(define (compute-e k)
  (+ 2 (cont-frac (lambda (i) 1.0)
                  (lambda (i) (if (= 2 (remainder i 3)) (* 2 (+ 1 (floor (/ i 3)))) 1)) ;; Di
                  k)))
(compute-e 100) ;; 2.7182818284590455

; exercise 1.39
(define (tan-cf x k)
  (cont-frac (lambda (i) (if (= i 1) x (- (* x x)))) ;; Ni
             (lambda (i) (- (* i 2) 1)) ;; Di
             k))
(tan-cf 3.14159 100)   ;; -2.6535897933138355e-006(约等于0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值