Fibonacci series in JavaScript
What is the Fibonacci series?
Fibonacci series is the series of numbers that are calculated by adding the values of the previous two numbers in the sequence. The first two numbers of the sequence are zero and one. It has a lot of applications in various fields and also it is one of the most asked interview questions. It is also known as the golden ratio. We can represent the Fibonacci sequence as shown below.
F(n) = F(n-1) + F(n-2), where F(0) = 0 and F(1) = 1 as the first two numbers of the sequence.
The integer sequence of the Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765 and goes on. Our aim is to write a javascript code for returning the Fibonacci sequence according to our input shown below.
Input : 3
Output : 3
Input : 9
Output : 55
In JavaScript, we can print the Fibonacci series in many ways. Let us discuss some of them.
1) Using for loop
A for loop repeats its statements until a specified condition evaluates to false. Let us see the code of the Fibonacci series using the for loop.
function fibonacci(num)
{
var num1 = 0;
var num2 = 1;
var sum;
var i = 0;
for (i = 0; i < num; i++)
{
sum = num1 + num2;
num1 = num2;
num2 = sum;
}
return num2;
}
document.write(fibonacci(1));
document.write(fibonacci(12));
Output:
1
233
Here 0 is printed first and then, in each iteration, the value of the second term is stored in variable num1 and the sum of two previous terms is stored in variable num2.
2) Using while loop
As we know, the while statement creates a loop that is executed while a condition is true and stops when the condition is false. Let us see the code of the Fibonacci series using the while loop.
function fibonacci(num)
{
if(num == 1)
return 0;
if(num == 2)
return 1;
var num1 = 0;
var num2 = 1;
var sum;
var i = 2;
while (i < num)
{
sum = num1 + num2;
num1 = num2;
num2 = sum;
i += 1;
}
return num2;
}
document.write(fibonacci(1));
document.write(fibonacci(12));
Output:
0
89
We can also write a Javascript code to find the Fibonacci sequence up to a certain number as shown below.
const number = parseInt(prompt('Enter a positive number: '));
let n1 = 0, n2 = 1, nextTerm;
console.log('Fibonacci Series:');
console.log(n1); // print 0
console.log(n2); // print 1
nextTerm = n1 + n2;
while (nextTerm <= number) {
// print the next term
console.log(nextTerm);
n1 = n2;
n2 = nextTerm;
nextTerm = n1 + n2;
}
Output:
Enter a positive number: 3
Fibonacci Series:
0
1
1
2
3
5
3) Using recursion
In recursion, the function calls itself until the program achieves the desired result. It should at least have one condition where it can stop calling itself or else the program will not stop calling itself until an error is thrown. We use F(n) = F(n-1) + F(n-2), where F(0) = 0 and F(1) = 1 as the first two numbers of the sequence. Let us see the code of the Fibonacci series using recursion.
function fibonacci(num)
{
if(num==1)
return 0;
if (num == 2)
return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
}
document.write(fibonacci(1));
document.write(fibonacci(12));
Output:
0
89
4) Binet’s formula
We can also calculate Fibonacci numbers using Binet’s formula. Let us see it in the code.
function binet(n) {
return Math.round((Math.pow((1 + Math.sqrt(5)) / 2, n) - Math.pow((1 - Math.sqrt(5)) / 2, n)) / Math.sqrt(5));
}
Conclusion
This was all about printing the Fibonacci series in JavaScript in various ways. Put your questions in the comments below and check out more articles related to javascript and other languages on Codedamn.
Sharing is caring
Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: