Factorial of a number is a non-negative integer calculated as the product of all positive integers less than or equal to the number and is denoted by n!.
For example: 10! = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3628800.
It can be represented by the following formula: n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Note that 0! = 1, according to the empty product convention.
Today we will learn two approaches to calculate the Factorial of a number in JavaScript: Iterative and Recursive.
Iterative Approach calculates the Factorial using a for loop:
const factorial = n => {
if(n < 0) {
throw new Error("Negative numbers are not allowed");
}
let result = 1;
if(n <= 1) {
return result;
}
for(let i = n; i > 1; i--) {
result = result * i;
}
return result;
};
Let's explain the above example:
The time complexity is O(n).
The space complexity is O(1) because it does not matter if we run factorial(10) or factorial(100), the space required remains the same.
Recursive Approach calculates the Factorial by calling the function on itself:
const factorial = n => {
if(n < 0) {
throw new Error("Negative numbers are not allowed");
}
if(n <= 1) {
return 1;
}
return n * factorial(n - 1);
};
The first two conditions are similar to those in Iterative Approach, but the ending is different: we multiply the provided n value by the result of executing the factorial function with the previous number.
The time complexity is O(n).
The space complexity is O(n).
We can use the Memoization technique to store the results of factorial function executions in the variable to ensure that the function is called only once per number, then the value is read from the cache:
let cache = {};
const factorial = n => {
if(n < 0) {
throw new Error("Negative numbers are not allowed");
}
if(n <= 1) {
return 1;
}
if(cache[n]) {
return cache[n];
}
const result = n * factorial(n - 1);
cache[n] = result;
return result;
};
It doesn't really add any improvements until we call the factorial function multiple times with the same arguments.
Both the Iterative and Recursive approaches are very simple, but use slightly different techniques to calculate the Factorial.
So which approach is better?
The recommended one is Iterative because it works faster.
Try to use it as often as possible.
In this article we learned how to find the Factorial of a given number.
This is one of the topics that are typically part of job interviews, so make sure you know how to implement and explain both approaches without any hints.