fast calculation of fibonacci numbers in python

I needed this for a challenge. If you need to reuse the values, it’s much faster with the memo.

#!/usr/bin/python                                                                                                                                             
memo = {0:0, 1:1}
def fib(n):
    if not n in memo:
        memo[n] = fib(n-1) + fib(n-2)
    return memo[n]
print fib(100)
$./fib.py
354224848179261915075

Leave a Reply