Fibonacci like sequence in JavaScript


In the given problem statement we are asked to create a fibonacci like sequence with the help of javascript functionalities. In the Javascript we can solve this problem with the help of recursion or using a for loop.

What is the Fibonacci Sequence ?

Let's understand the working of a Fibonacci sequence in JavaScript.

As we know that Fibonacci sequence is a sequence of numbers in which every number is the sum of the two forgoing numbers. The series starts from 0 and 1. The next number in the series is 1 (0 + 1), followed by 2 (1 + 1), 3 (1 + 2)....end and so on.

Logic for The Above Problem

The easiest way to implement the fibonacci sequence is with the help of a recursive method.

So let's understand the logic of the problem given. The recursive method of the fibonacci sequence in javascript breaks down the problem into smaller subproblems. And it starts by checking if the input number is less than or equal to 1. If it is equal to 1 then it returns that number otherwise it's not. Then it will calculate the nth Fibonacci number by recursively calling the defined function with n-1 and n−2 as arguments and add the results together.

Algorithm

Step 1 − Declare a number till up to get the fibonacci sequence or to generate sequence to the n terms. Here we will see both the implementations.

Step 2 − Declare the n1 and n2 numbers to 0 and 1 respectively. And define a nextDigit number as empty. Here nextDigit will be the sum of two numbers n1 and n2.

Step 3 − This step will calculate and determine the method we are using for both the code. If we want to get the output up to a certain number then we will use a while loop, this loop will run until the condition, nextDigit is less than or equal to the number, print the sequence. If we want the output up to the n terms then we will use a for loop to iterate through the length of the given input number.

Step 4 − Now forwarding to the third step, print the Fibonacci sequence as required.

Example

// code to generate fibonacci sequence up to a certain number
const number = 13;
let n1 = 0, n2 = 1, nextDigit;

console.log('Fibonacci Series is as follows:');
// print 0 and 1 initially
console.log(n1);
console.log(n2);

//adding two consecutive digits
nextDigit = n1 + n2;

while (nextDigit <= number) {

    // print the next digit
    console.log(nextDigit);

    n1 = n2;
    n2 = nextDigit;
    nextDigit = n1 + n2;
}

Output

Fibonacci Sequence is as follows:
0
1
1
2
3
5
8
13

Example 

// code to generate fibonacci series up to n terms
const number = 8;
let n1 = 0, n2 = 1, nextDigit;

console.log('Fibonacci Sequence is as follows:');

//initialize a for loop
for (let i = 1; i <= number; i++) {
    console.log(n1);
    nextDigit = n1 + n2;
    n1 = n2;
    n2 = nextDigit;
}

Output

Fibonacci Sequence i as follows:
0
1
1
2
3

Time Complexity

The time complexity for generating up to a certain number will be O(log n). Because code uses a while loop to get the sequence up to a given number. This loop continues until the nextDigit is greater than the input number, at that point the while loop will get terminated. Since the while loop iterates log base 2 of n times that is why the complexity for this code will be O(log n). The space complexity of the first code is O(1) because the space required does not depend on the input size.

The time complexity for the second method is O(n), n is the number of terms in the sequence. This block of code uses a for loop to iterate all the range of numbers from 1 to the input number. Since the loop is running till n number of times so complexity will be O(n).

The space complexity of the second method is also O(n) because it is storing the values n1 and n2 for every step of the loop. Since the memory required to these numbers is constant and it is not depending upon the input size. So in that case we ignore analyzing the space complexity.

Conclusion

In this code we have implemented two kinds of code or algorithm. First code gives the output to print Fibonacci sequence up to a certain number. And the second code returns the output for n terms. And we have concluded that the time complexity for first and second methods are O(log n) and O(n) respectively. The space complexity for both the code is O1).

Updated on: 23-Aug-2023

267 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements