問題1.31

今日は比較的順調。

product手続き
(define (product term a next b)
  (if (> a b)
	  1
	  (* (term a)
   	     (product term (next a) next b))))
productを使ったfactorial
(define (factorial n)
  (define (term x) x)
  (define (next n) (+ n 1))
  (product term 1 next n))

まだ、この辺は、コード見てても想像がつく。

πの近似値
(define (pi n)
  (define (square x) (* x x))
  (define (next n) (+ n 2))
  (define (term n)
	(/ (* n (+ n 2))
	   (square (+ n 1))))
  (* 4.0 
	 (product term 2 next n)))

termがちょい迷った。2項ずつ考えていけばいいんだね。

ところで、この式ってあんまし知らないんだけど、有名なんだろうか?
Wikipediaを見ると、1/4円の面積を求める時に、挟み撃ちのような手法をとると、このような式に展開されるのかな?
明日、数学好きの人に聞いてみよう。

反復プロセス
(define (product-iter term a next b)
  (define (iter a result)
	(if (> a b)
	    result
     	    (iter (next a) (* result (term a)))))
  (iter a 1))