Suppose in a Class, the Teacher asked students of roll number 1 to write 0 and roll number 2 to write 1 on the blackboard and asked for the rest of the students, to write the summation of your previous two students’. The series written on the board will look like 0,1,1,2,3,5,8,……….
The teacher then told the students, this series is known as the Fibonacci series. It can be represented by the below equation
Fn = Fn-1 + Fn-2
Where F0=1 and F1=1. The Fibonacci numbers are the numbers in the following integer sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, âĶâĶ..In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1
Examples:
Input : 5 Output : 8 Input :8 Output :34
As the first Fibonacci number is 0 and the second is 1. Also, we know that the nth Fibonacci number is the summation of n-1 and n-2 term.
So, to get the nth Fibonacci term we can follow
fib(1)=0
fib(2)=1
fib(3)=fib(2)+fib(1)
fib(4)=fib(3)+fib(2)
….
fib(n)=fib(n-1)+fib(n-2)
Example: By using for loop
javascript
<script type = "text/javascript"> 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(5): "+fibonacci(5)+"<br>"); document.write("Fibonacci(8): "+fibonacci(8)+"<br>"); </script> |
Output:
Fibonacci(5): 3 Fibonacci(8): 13
Example: By using while loop
javascript
<script type = "text/javascript"> 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(5): "+fibonacci(5)+"<br>"); document.write("Fibonacci(8): "+fibonacci(8)+"<br>"); </script> |
Output:
Fibonacci(5): 3 Fibonacci(8): 13
By using recursion: As we know that the nth Fibonacci number is the summation of n-1 and n-2 term and the n-1 term is the summation of n-2 and n-3 term.
So, to get the nth Fibonacci term we can follow
fib(n)=fib(n-1)+fib(n-2)
fib(n)=fib(n-2)+fib(n-3)+fib(n-3)+fib(n-4)
….
fib(n)=fib(1)+fib(0)+fib(1)+fib(0)+fib(1)+fib(0)….fib(1)+fib(0) [terms containing sum of fib(1) and fib(0)
fib(1)=0
fib(2)=1
Example:
javascript
<script type = "text/javascript"> function fibonacci(num) { if(num==1) return 0; if (num == 2) return 1; return fibonacci(num - 1) + fibonacci(num - 2); } document.write("Fibonacci(5): "+fibonacci(5)+"<br>"); document.write("Fibonacci(8): "+fibonacci(8)+"<br>"); </script> |
Output:
Fibonacci(5): 3 Fibonacci(8): 13
Recommended Posts:
- PHP | Fibonacci Series
- PHP program to print an arithmetic progression series using inbuilt functions
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- Javascript | Program to read text File
- JavaScript | Program to generate one-time password (OTP)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


