Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Fibonacci like sequence in JavaScript
In JavaScript, you can generate a Fibonacci-like sequence using various approaches including loops and recursion. The Fibonacci sequence is fundamental in programming and demonstrates different algorithmic techniques.
What is the Fibonacci Sequence?
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding numbers. The sequence starts with 0 and 1, then continues as: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.
Formula: F(n) = F(n-1) + F(n-2), where F(0) = 0 and F(1) = 1
Method 1: Generate Fibonacci Up to a Certain Number
This approach generates all Fibonacci numbers that are less than or equal to a given limit:
const number = 13;
let n1 = 0, n2 = 1, nextDigit;
console.log('Fibonacci Series up to', number + ':');
console.log(n1); // Print 0
console.log(n2); // Print 1
nextDigit = n1 + n2;
while (nextDigit <= number) {
console.log(nextDigit);
n1 = n2;
n2 = nextDigit;
nextDigit = n1 + n2;
}
Fibonacci Series up to 13: 0 1 1 2 3 5 8 13
Method 2: Generate First N Fibonacci Numbers
This approach generates exactly N terms of the Fibonacci sequence:
const number = 8;
let n1 = 0, n2 = 1, nextDigit;
console.log('First', number, 'Fibonacci numbers:');
for (let i = 1; i <= number; i++) {
console.log(n1);
nextDigit = n1 + n2;
n1 = n2;
n2 = nextDigit;
}
First 8 Fibonacci numbers: 0 1 1 2 3 5 8 13
Method 3: Recursive Approach
A recursive implementation that calculates the nth Fibonacci number:
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
// Generate first 8 Fibonacci numbers using recursion
console.log('Fibonacci using recursion:');
for (let i = 0; i < 8; i++) {
console.log(fibonacci(i));
}
Fibonacci using recursion: 0 1 1 2 3 5 8 13
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Up to Number (While Loop) | O(log ? n)* | O(1) | Finding all Fibonacci numbers below a limit |
| First N Terms (For Loop) | O(n) | O(1) | Generating specific number of terms |
| Recursive | O(??) | O(n) | Educational purposes (inefficient for large n) |
*? (phi) ? 1.618 is the golden ratio
Key Points
- Iterative approaches are more efficient than recursion for Fibonacci generation
- The while loop method is useful when you need all Fibonacci numbers below a threshold
- The for loop method is ideal when you know exactly how many terms you need
- Recursive solution is elegant but has exponential time complexity due to repeated calculations
Conclusion
JavaScript offers multiple ways to generate Fibonacci sequences. Use iterative approaches for performance and recursive methods for learning algorithm concepts. The choice depends on whether you need a specific count of terms or all terms below a given number.
