Moved 1 to 5 to Python and learned a ton.
This commit is contained in:
44
python/lib_fibonacci.py
Normal file
44
python/lib_fibonacci.py
Normal file
@@ -0,0 +1,44 @@
|
||||
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
|
||||
Reference in New Issue
Block a user