Generate Fibonacci Sequence

Updated on 30 May, 2025
Generate Fibonacci Sequence header image

Problem Statement

In this challenge, we're asked to create a generator function that, when invoked, provides a generator object. This object, when used, should generate numbers in the Fibonacci sequence. For those unfamiliar, the Fibonacci sequence is a series of numbers where each subsequent number is derived by adding the two preceding ones. The sequence typically commences with the integers 0 and 1. According to the definition, the nth Fibonacci number (assuming the sequence starts with index 0) is calculated using the formula Xn = Xn-1 + Xn-2. The challenge specifies the requirement of generating this sequence dynamically up to a user-defined number of calls (callCount).

Examples

Example 1

Input:

callCount = 5

Output:

[0,1,1,2,3]

Explanation:

const gen = fibGenerator();
gen.next().value; // 0
gen.next().value; // 1
gen.next().value; // 1
gen.next().value; // 2
gen.next().value; // 3

Example 2

Input:

callCount = 0

Output:

[]

Explanation:

gen.next() is never called so nothing is outputted

Constraints

  • 0 <= callCount <= 50

Approach and Intuition

The Fibonacci sequence, while simple in definition, provides a quintessential example of how generators can be efficiently implemented to handle potentially infinite sequences up to a certain limit. For this challenge, we are provided with variable calls, determined by callCount, where callCount could be any value from 0 to 50. Based on its value, the generator should yield that many numbers from the Fibonacci sequence.

  • Implementation steps for the generator function:

    1. Initialize the two starting numbers of the Fibonacci sequence (current = 0 and next = 1).
    2. For each call, yield the current number which is the current Fibonacci number.
    3. Update the values of current and next to move to the next set of values in the sequence: set current to next, and next to the sum of the old current and next.
  • Example Breakdown:

    • Example 1:

      • Input: callCount = 5
      • Output: [0, 1, 1, 2, 3]
      • Explanation: If callCount is 5, the generator is invoked 5 times consequently yielding the first five Fibonacci numbers. Starting from 0 and 1, the function computes each subsequent number by summing the previous two numbers in the sequence.
    • Example 2:

      • Input: callCount = 0
      • Output: []
      • Explanation: Here, the generator function is initialized, but it is never called to yield a value. As a result, no part of the Fibonacci sequence is generated or outputted.

The usage of a generator allows this Fibonacci sequence generation to be implemented in an efficient manner that does not utilize excessive memory, even for the upper limit of 50 calls.

Solutions

  • JavaScript
js
var generateFibonacci = function() {
    const totalFibs = 50;

    const fibs = Array(totalFibs).fill(0);

    fibs[1] = 1;

    for (let i = 2; i < fibs.length; i++) {
        fibs[i] = fibs[i - 1] + fibs[i - 2];
    }

    return fibs[Symbol.iterator]();
};

Generate the Fibonacci sequence in JavaScript by defining a function generateFibonacci. Initialize a container for the first 50 numbers of the sequence using an array pre-filled with zeros. Modify the second item of this array to 1, reflecting the sequence's start.

Iterate through the array starting from the third item. For each position, sum the two preceding numbers to find the new Fibonacci number. After populating the array, convert it to an iterator for seamless traversal of the sequence through simple iteration methods. This setup allows retrieval of Fibonacci numbers up to the 50th in the series using a concise and direct approach.

Comments

No comments yet.