Created
November 1, 2015 16:45
-
-
Save ewhitebloom/102d0ddc762751f8ab37 to your computer and use it in GitHub Desktop.
Fibonacci
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
def recursiveFibonacci(n) | |
if n == 0 || n == 1 | |
n | |
else | |
recursiveFibonacci(n-1) + recursiveFibonacci(n-2) | |
end | |
end | |
def iterativeFibonacci(n) | |
cache = [] | |
for i in 0..n do | |
if i == 0 || i == 1 | |
cache << i | |
else | |
cache << cache.shift + cache.first | |
end | |
end | |
cache.last | |
end | |
print recursiveFibonacci(0) | |
puts iterativeFibonacci(0) | |
print recursiveFibonacci(1) | |
puts iterativeFibonacci(1) | |
puts recursiveFibonacci(10) | |
puts iterativeFibonacci(10) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment