Skip to content

Instantly share code, notes, and snippets.

@StabbyMcDuck
Created September 29, 2016 17:14
Show Gist options
  • Save StabbyMcDuck/97e2fbb6ed3653f3bcdb5396083023d6 to your computer and use it in GitHub Desktop.
Save StabbyMcDuck/97e2fbb6ed3653f3bcdb5396083023d6 to your computer and use it in GitHub Desktop.
recursive fibonacci in python
from time import time
fibs = {0: 0, 1: 1}
def fib(n):
if n in fibs: return fibs[n]
if n % 2 == 0:
fibs[n] = ((2 * fib((n / 2) - 1)) + fib(n / 2)) * fib(n / 2)
return fibs[n]
else:
fibs[n] = (fib((n - 1) / 2) ** 2) + (fib((n+1) / 2) ** 2)
return fibs[n]
s=time()
# enter n in print fib(n)
print fib(5)
print time()-s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment