Skip to content

Instantly share code, notes, and snippets.

@ewhitebloom
Created November 1, 2015 16:45
Show Gist options
  • Save ewhitebloom/102d0ddc762751f8ab37 to your computer and use it in GitHub Desktop.
Save ewhitebloom/102d0ddc762751f8ab37 to your computer and use it in GitHub Desktop.
Fibonacci
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