leetcode: solve problem 2648

This commit is contained in:
Ivan R. 2024-02-07 21:48:43 +05:00
parent 48587158b2
commit b2e06356b0
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
2 changed files with 28 additions and 0 deletions

View file

@ -0,0 +1,19 @@
/**
* const gen = fibGenerator();
* gen.next().value; // 0
* gen.next().value; // 1
*/
function* fibGenerator(): Generator<number, any, number> {
let a = 0;
let b = 1;
let iter = 0;
while (true) {
if (iter < 2) {
yield iter;
iter++;
} else {
yield a + b;
[a, b] = [b, a + b];
}
}
};

View file

@ -0,0 +1,9 @@
# 2648. Generate Fibonacci Sequence
[Leetcode](https://leetcode.com/problems/generate-fibonacci-sequence/description/)
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.