The Fibonacci series is a famous mathematical sequence where each number is the sum of the two preceding numbers. It is widely used in algorithms, programming challenges, and real-world applications like cryptography and computer graphics.
In this blog, weβll explore:
β
What the Fibonacci series is
β
How to generate it in JavaScript using loops and recursion
β
Performance considerations for different implementations
The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the previous two numbers.
π’ Fibonacci Series Example:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
Mathematically, it is defined as:
F(n) = F(n-1) + F(n-2)
where:
F(0) = 0
F(1) = 1
Now, let's see how to implement this in JavaScript.
The iterative approach is efficient in terms of time complexity (O(n)) and avoids recursion overhead.
function fibonacciSeries(n) {
let fib = [0, 1];
for (let i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib;
}
// Example Usage
console.log(fibonacciSeries(10));
// Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
We initialize an array with [0, 1]
.
We use a for
loop to compute each Fibonacci number by summing the previous two numbers.
The function returns an array of the first n
Fibonacci numbers.
β
Pros: Fast and memory-efficient.
β Cons: Requires storing all numbers in an array.
The recursive method follows the mathematical definition but is inefficient due to repeated calculations.
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
// Example Usage
console.log(fibonacci(10));
// Output: 55 (10th Fibonacci number)
Base case: If n
is 0
or 1
, return n
.
Recursive case: Compute fibonacci(n-1) + fibonacci(n-2)
.
β Cons: This approach is very slow (O(2^n)
time complexity) due to redundant calculations.
We can optimize the recursive approach using memoization to store previously computed values.
function fibonacciMemo(n, memo = {}) {
if (n in memo) return memo[n];
if (n <= 1) return n;
memo[n] = fibonacciMemo(n - 1, memo) + fibonacciMemo(n - 2, memo);
return memo[n];
}
// Example Usage
console.log(fibonacciMemo(10));
// Output: 55
By storing already computed Fibonacci numbers, we reduce time complexity from O(2^n) to O(n).
A mathematical approach based on Binet's formula can compute Fibonacci numbers in constant time O(1).
function fibonacciFormula(n) {
const sqrt5 = Math.sqrt(5);
const phi = (1 + sqrt5) / 2;
return Math.round(Math.pow(phi, n) / sqrt5);
}
// Example Usage
console.log(fibonacciFormula(10));
// Output: 55
β
Fastest approach (O(1) time complexity)
β Not ideal for generating a series, only for finding individual Fibonacci numbers.
In this blog, we explored different ways to implement the Fibonacci series in JavaScript.
π For generating a series β Use the iterative method.
π For finding a single Fibonacci number β Use Binet's formula.
π For optimized recursion β Use memoization.
π Try implementing these methods and experiment with larger values to compare performance! π