From b2e06356b0d086886706c07529e66c250c5d55fc Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Wed, 7 Feb 2024 21:48:43 +0500 Subject: [PATCH] leetcode: solve problem 2648 --- .../2648-generate-fibonacci-sequence/main.ts | 19 +++++++++++++++++++ .../readme.md | 9 +++++++++ 2 files changed, 28 insertions(+) create mode 100644 leetcode/2648-generate-fibonacci-sequence/main.ts create mode 100644 leetcode/2648-generate-fibonacci-sequence/readme.md diff --git a/leetcode/2648-generate-fibonacci-sequence/main.ts b/leetcode/2648-generate-fibonacci-sequence/main.ts new file mode 100644 index 0000000..542af5a --- /dev/null +++ b/leetcode/2648-generate-fibonacci-sequence/main.ts @@ -0,0 +1,19 @@ +/** + * const gen = fibGenerator(); + * gen.next().value; // 0 + * gen.next().value; // 1 + */ +function* fibGenerator(): Generator { + 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]; + } + } +}; diff --git a/leetcode/2648-generate-fibonacci-sequence/readme.md b/leetcode/2648-generate-fibonacci-sequence/readme.md new file mode 100644 index 0000000..aa7c5a5 --- /dev/null +++ b/leetcode/2648-generate-fibonacci-sequence/readme.md @@ -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.