RosettaCodeData/Task/Fibonacci-sequence/Python/fibonacci-sequence-13.py

12 lines
233 B
Python
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
def fib():
2026-02-01 16:33:20 -08:00
"""Yield fib[n+1] + fib[n]"""
2023-07-01 11:58:00 -04:00
yield 1
2026-02-01 16:33:20 -08:00
lhs, rhs = fib(), fib()
# move lhs one iteration ahead
yield next(lhs)
2023-07-01 11:58:00 -04:00
while True:
2026-02-01 16:33:20 -08:00
yield next(lhs)+next(rhs)
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
f=fib()
print [next(f) for _ in range(9)]