Skip to content

Instantly share code, notes, and snippets.

@cdelahousse
Created January 18, 2012 07:27
Show Gist options
  • Save cdelahousse/1631784 to your computer and use it in GitHub Desktop.
Save cdelahousse/1631784 to your computer and use it in GitHub Desktop.
Fibonacci Implementation
;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