45 lines
812 B
Python
45 lines
812 B
Python
|
def fibonacci_generator():
|
||
|
"""
|
||
|
Fibonacci generator function that starts with 1, 1, 2, 3, ...
|
||
|
|
||
|
:returns: generator that yields fibonacci numbers
|
||
|
"""
|
||
|
a = 0
|
||
|
b = 1
|
||
|
yield b
|
||
|
while True:
|
||
|
a, b = b, (a + b)
|
||
|
yield b
|
||
|
|
||
|
|
||
|
def fibonacci_generator_smaller(n):
|
||
|
"""
|
||
|
|
||
|
:param n: generator yields all fibonacci numbers smaller n
|
||
|
:returns: generator that yields fibonacci numbers
|
||
|
|
||
|
"""
|
||
|
g = fibonacci_generator()
|
||
|
x = next(g)
|
||
|
while x < n:
|
||
|
yield x
|
||
|
x = next(g)
|
||
|
|
||
|
|
||
|
def fibonacci_nth(n):
|
||
|
"""
|
||
|
|
||
|
:param n: index of fibonacci that should be returned
|
||
|
:returns: nth fibonacci number
|
||
|
|
||
|
"""
|
||
|
if n < 1:
|
||
|
return 0
|
||
|
g = fibonacci_generator()
|
||
|
i = 1
|
||
|
x = next(g)
|
||
|
while i < n:
|
||
|
i += 1
|
||
|
x = next(g)
|
||
|
return x
|