2017-12-08 20:29:13 +01:00
|
|
|
"""
|
|
|
|
**ćwiczenie 2**
|
|
|
|
Napisz generator, który będzie zwracać ``n`` kolejnych liczb ciągu Fibonacciego (``F(0)=1, F(1)=1, FN=F(N-1) + F(N-2)``).
|
|
|
|
"""
|
|
|
|
|
2017-12-03 14:50:19 +01:00
|
|
|
def Fibogen(n):
|
|
|
|
if n == 0:
|
2017-12-08 20:29:13 +01:00
|
|
|
yield 1
|
2017-12-03 14:50:19 +01:00
|
|
|
if n == 1:
|
2017-12-08 20:29:13 +01:00
|
|
|
yield 1
|
|
|
|
yield 1
|
|
|
|
if n >= 2 :
|
|
|
|
i, j = 1, 1
|
|
|
|
yield 1
|
|
|
|
yield 1
|
|
|
|
for k in range(2, n):
|
|
|
|
f = i + j
|
|
|
|
i = j
|
|
|
|
j = f
|
|
|
|
yield f
|
|
|
|
|
2017-12-03 14:50:19 +01:00
|
|
|
|
2017-12-08 20:29:13 +01:00
|
|
|
for i in Fibogen(15):
|
2017-12-03 14:50:19 +01:00
|
|
|
print(i)
|
|
|
|
|
|
|
|
|