You are required to build a Python generator that produces a sequence of Fibonacci numbers. How would you implement the generator to yield the Fibonacci sequence efficiently?
- Create a list of all Fibonacci numbers and return it as a generator.
- Implement the generator using a recursive approach to calculate Fibonacci numbers.
- Use a loop to generate Fibonacci numbers and yield them one by one.
- Use a stack data structure to generate Fibonacci numbers efficiently.
To generate Fibonacci numbers efficiently, you should use a loop and yield each Fibonacci number one by one. The recursive approach (Option 1) is inefficient due to repeated calculations.
Loading...
Related Quiz
- The while loop continues execution as long as the test expression remains _______.
- How do you instantiate an object from a class in Python?
- For which of the following OS is Python NOT available by default?
- In Seaborn, the ____ function is used to plot univariate or bivariate distributions of observations.
- Which sorting algorithm is best suited for large datasets?