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]
def test(n):
count = range(0,n)
seq = []
for i in count:
seq.append(fib(i))
return seq
print test(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
如何使它从 1 而不是 0 开始所以结果是
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
我尝试将 fibs 更改为 {1:1,2:2} 但它没有用