[archives] [homepage]

Problem 1.11
Friday, August 23, 2019; ago; Download .md

SICP Exercise 1.11

Recursive:

(define (f n)
  (cond ((< n 3) n)
      (else (+ (f (- n 1))
               (* 2 (f (- n 2)))
               (* 3 (f (- n 3)))))))

Iterative:

(define (f n) 
  (define (iter a b c count) 
    (if (= count 0) 
      a 
      (iter b c (+ c (* 2 b) (* 3 a)) (- count 1)))) 
  (iter 0 1 2 n))