I'm having troubles try to create a Fast doubling Fibonacci python generator, using the following.
Given F(k) and F(k+1), we can calculate these:
F(2k) = F(k)[2F(k + 1) − F(k)]
F(2k+1) = F(k+1)^2 + F(k)^2
I've got the following for the simplest (slow) Fibonacci generator:
def fib_generator():
n = 1
n0 = 1
while True:
yield n
n, n0 = n + n0, n
An implementation could be:
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
First results:
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
First get a list of the fibonacci sequence... Then loop through.
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=' ')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.