3 ms·
def fib(n): x = ((1 + sqrt(5)) / 2) ** n x -= ((1 - sqrt(5)) / 2) ** n return x / sqrt(5)
by sillysaurus3 13y ago
def fib(n):
x = ((1 + sqrt(5)) / 2) ** n
x -= ((1 - sqrt(5)) / 2) ** n
return x / sqrt(5)
- mjcohen 13y agoTry n=1000 or n=10000.
- sillysaurus3 13y agofrom math import sqrt from decimal import Decimal def fib(n): x = Decimal((1 + sqrt(5)) / 2) ** n x -= Decimal((1 - sqrt(5)) / 2) ** n return x / Decimal(sqrt(5)) print fib(1000) print fib(10000) print fib(1290309123) $ time python ~/fib.py 4.346655768693891359691727380E+208 3.364476487644307695949089018E+2089 2.879417086171668653426018537E+269658658 real 0m0.138s user 0m0.086s sys 0m0.026s