Generate Fibonacci Sequence - Problem
Write a generator function that returns a generator object which yields the Fibonacci sequence.
The Fibonacci sequence is defined by the relation Xn = Xn-1 + Xn-2.
The first few numbers of the series are: 0, 1, 1, 2, 3, 5, 8, 13...
Your generator should yield numbers indefinitely when called repeatedly.
Input & Output
Example 1 — First 6 Numbers
$
Input:
n = 6
›
Output:
[0,1,1,2,3,5]
💡 Note:
The first 6 Fibonacci numbers: F(0)=0, F(1)=1, F(2)=0+1=1, F(3)=1+1=2, F(4)=1+2=3, F(5)=2+3=5
Example 2 — First 8 Numbers
$
Input:
n = 8
›
Output:
[0,1,1,2,3,5,8,13]
💡 Note:
Extending further: F(6)=3+5=8, F(7)=5+8=13. Each number is sum of previous two.
Example 3 — Edge Case
$
Input:
n = 1
›
Output:
[0]
💡 Note:
Only the first Fibonacci number, which is 0 by definition.
Constraints
- 1 ≤ n ≤ 50
- Generated numbers will fit in 64-bit integer
Visualization
Tap to expand
Understanding the Visualization
1
Input
Number n indicating how many Fibonacci numbers to generate
2
Process
Use relation F(n) = F(n-1) + F(n-2) with F(0)=0, F(1)=1
3
Output
Array containing first n Fibonacci numbers
Key Takeaway
🎯 Key Insight: Only the previous two numbers are needed to generate the next Fibonacci number
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code