2015/10/08
SICP 問題1.31
;; 再帰的プロセスでproduct
(define (product term a next b)
(if (> a b)
0
(* (term a)
(product term (next a) next b))))
;; 反復的プロセスでproduct
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* (term a) result))))
(iter a 1))
(define (factorial n)
(product identity 1 1+ n))
(define (pi-product n)
(define (term i)
(if (odd? i)
(/ (+ i 1) (+ i 2))
(/ (+ i 2) (+ i 1))))
(product term 1 1+ n))
(define (pi n)
(* 4 (pi-product n)))