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.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *