Middle+
Write a Fibonacci number generator.
sobes.tech AI
Answer from AI
The Fibonacci number generator is a function that returns the next number in the sequence each time it is called, where each number is the sum of the two preceding ones, starting from 0 and 1.
Example in Python:
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Usage:
gen = fibonacci_generator()
for _ in range(10):
print(next(gen))
This generator infinitely produces Fibonacci numbers on demand.