22 lines
348 B
Python
22 lines
348 B
Python
|
from math import sqrt
|
||
|
|
||
|
def pq(p,q):
|
||
|
a = -p/2
|
||
|
b = sqrt(a**2-q)
|
||
|
return (a+b,a-b)
|
||
|
|
||
|
(l1,l2) = pq(-1,-1)
|
||
|
c2 = -l1/(l2-l1)
|
||
|
c1 = 1-c2
|
||
|
|
||
|
c1 *= l1
|
||
|
c2 *= l2
|
||
|
|
||
|
fib = lambda k: int(c1*pow(l1,k) + c2*pow(l2,k))
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
print("fib(k) = (%f) * (%f)^k + (%f) * (%f)^k)" % (c1,l1,c2,l2))
|
||
|
|
||
|
for i in range(0,51):
|
||
|
print("%d: %d" %(i,fib(i)))
|
||
|
y
|