Write a generator function `fibonacci()` that yields Fibonacci numbers indefinitely.
Problem Statement
Write a generator function `fibonacci()` that yields Fibonacci numbers indefinitely.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Generator functions use function* and yield.
š” Hint 2:
Fibonacci: each number is the sum of the previous two.
š” Hint 3:
let a = 0, b = 1; while (true) { yield a; [a, b] = [b, a + b]; }
ā
Solution:
```javascript
function* fibonacci() {
let a = 0, b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
```