
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
The Fibonacci sequence in Javascript
Fibonacci numbers are the numbers such that every number in the series after the first two is the sum of the two preceding ones. The series starts with 1, 1. Example −
1, 1, 2, 3, 5, 8, 13, 21, 34, ….
We can write a program to generate nth as follows −
functionfibNaive(n) { if (n<= 1) return n; returnfibNaive(n - 1) + fibNaive(n - 2); }
You can test this using −
console.log(fibNaive(7)); console.log(fibNaive(8)); console.log(fibNaive(9)); console.log(fibNaive(4));
This will give the output −
13 21 34 3
Let us look at how these function calls are actually happening −
/** * f(5) * / \ * f(4) f(3) * / \ / \ * f(3) f(2) f(2) f(1) * / \ .......... * f(2) f(1).......... */
When we make a call to f(5), we will be calling f(2) nearly 4 times and it'll run the same code over and over 4 times. This is a case of an overlapping subproblem. Try running that function for 500. You'll be stuck as all these calls will take loads of time.
When we need the 5th Fibonacci number, we just need the lower Fibonacci numbers once but we compute them a lot more times than that. We can reduce this redundant computation if we just store the computed values somewhere instead. This is the crux of dynamic programming.
Compute once and reuse later.
Let's look at an memorized implementation of the fib function.
letfibStore = {}; functionfibDP(n) { if (n<= 1) return n; if (fibStore[n]) { returnfibStore[n]; } fibStore[n] = fibDP(n - 1) + fibDP(n - 2); returnfibStore[n]; }
Now we're using a store, fibStore to keep track of values we've already computed. This reduces the excessive redundant computations and keeps the function efficient.
You can test this using −
console.log(fibDP(7)); console.log(fibDP(8)); console.log(fibDP(9)); console.log(fibDP(4));
This will give the output −
13 21 34 3
You can even test this for huge values.
- Related Articles
- Fibonacci like sequence in JavaScript
- Finding Fibonacci sequence in an array using JavaScript
- 8085 program to generate Fibonacci sequence
- 8086 program to generate Fibonacci Sequence
- How to print the Fibonacci Sequence using Python?
- Python Program to Display Fibonacci Sequence Using Recursion
- Checking for Fibonacci numbers in JavaScript
- Nth element of the Fibonacci series JavaScript
- Sum of even Fibonacci terms in JavaScript
- Finding the longest "uncommon" sequence in JavaScript
- JavaScript code for recursive Fibonacci series
- C++ Program to Search Sorted Sequence Using Divide and Conquer with the Aid of Fibonacci Numbers
- Validate a number as Fibonacci series number in JavaScript
- Strictly increasing sequence JavaScript
- Validating push pop sequence in JavaScript
