Created
January 18, 2012 07:27
-
-
Save cdelahousse/1631784 to your computer and use it in GitHub Desktop.
Fibonacci Implementation
This file contains 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
;Recursive | |
(define (fib n) | |
(cond ((eq? n 0) 0) | |
((eq? n 1) 1) | |
(else (+ (fib (- n 2)) | |
(fib (- n 1)))))) | |
;Iterative | |
(define (fib n) | |
(define (iter n-1 n-2 count) | |
(if (< n count) | |
n-1 | |
(iter (+ n-1 n-2) n-1 (+ 1 count)))) | |
(iter 1 0 0)) | |
;What is it good for? | |
(define (approximate-golden-ratio n) | |
(cond ((eq? n 0) 0) | |
(else (exact->inexact (/ (fib (- n 1)) (fib (- n 2))))))) | |
(approximate-golden-ratio 30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment