sicp第1章の練習問題1.11体得再帰と反復
sicp第1章の練習問題1.11体得再帰と反復式f(x)がx>=3の場合f(x)=f(x-1)+2 f(x-2)+3 f(x-3)であり、x<3の場合、f(x)=xが再帰と反復で表される
再帰式
反復式
再帰式
(define (compute n)
(cond ((< n 0) 0)
((< n 3) n)
(else (+ (compute (- n 1))
(* 2 (compute (- n 2)))
(* 3 (compute (- n 3)))
)
)
)
)
反復式
(define (compute2 n)
(compute-iter 2 1 0 n))
(define (compute-iter a b c count)
(if (= count 0)
c
(compute-iter (+ a (* 2 b) (* 3 c))
a
b
(- count 1)
)
)
)