Skip to content

Instantly share code, notes, and snippets.

@vedantk
Created August 3, 2011 05:38
Show Gist options
  • Save vedantk/1121980 to your computer and use it in GitHub Desktop.
Save vedantk/1121980 to your computer and use it in GitHub Desktop.
An iterative version of f(n).
(define (f' n)
(define (f'-iter a b c count)
; f(n) = a + b + c = f(n-1) + 2f(n-2) + 3f(n-3).
; Use f(n) to calculate f(n+1) = f(n) + 2f(n-1) + 3f(n-2).
(if (= count (+ n 1))
a
(f'-iter (+ a b c) (* 2 a) (* 3 (/ b 2)) (+ count 1))))
(if (< n 3)
n
; f(3) = f(2) + 2f(1) + 3f(0).
(f'-iter 2 2 0 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment