快速加倍斐波那契 Python 生成器序列

Fast doubling Fibonacci Python generator sequence

我在尝试使用以下方法创建快速加倍斐波那契 python 生成器时遇到问题。

给定 F(k) 和 F(k+1),我们可以计算这些:

F(2k) = F(k)[2F(k + 1) − F(k)]
F(2k+1) = F(k+1)^2 + F(k)^2

我有以下最简单(慢速)的斐波那契数列生成器:

def fib_generator():
    n = 1
    n0 = 1
    while True:
        yield n
        n, n0 = n + n0, n

实现可以是:

from itertools import count

def fast_fib_generator():
    F = [1, 1]
    yield 1
    yield 1
    for k in count(1):
        F.append(F[k] ** 2 + F[k - 1] ** 2)
        yield F[-1]

        F.append(F[k] * (2 * F[k + 1] - F[k]))
        yield F[-1]


for x in fast_fib_generator():
    print x

第一个结果:

1
1
2
3
5
8
13
21
34  
def fibonacci(number):
    numbers = [0, 1]
    while len(numbers) < number:
        numbers[len(numbers):len(numbers)] = [numbers[len(numbers)-2] + numbers[len(numbers)-1]]
    return numbers

首先获取斐波那契数列的列表...然后循环。

def fib_gen(number):
    for iter in fibonacci(number):
        print iter

Try this

num=int(input("Enter length of series required"))
fiblist=[]
for i in range(num):
    if i==0 or i==1:
        fiblist.append(i)
    else:
        temp=fiblist[i-2]+fiblist[i-1]
        fiblist.append(temp)
print(*fiblist,sep=' ')