Created
August 3, 2011 05:38
-
-
Save vedantk/1121980 to your computer and use it in GitHub Desktop.
An iterative version of f(n).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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