In mathematics, the factorial of a non-negative integer n can be a tricky algorithm. In this article, I’m going to explain three approaches to factorize number, first with the recursive function, second using a while loop, and third using a for a loop.
We have already seen a recursion approach on a String in the previous article, Ten Ways to Reverse a String in JavaScript . This time we will apply the same concept on a number.
Algorithm Challenge
Return the factorial of the provided integer.
If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
Factorials are often represented with the shorthand notation n!
For example: 5! = 1 * 2 * 3 * 4 * 5 = 120
Provided test cases
1. factorialize(0) should return 1
2. factorialize(5) should return 120
3. factorialize(10) should return 3628800
4. factorialize(20) should return 2432902008176640000
What is Factorializing a number?
The factorial, symbolized by an exclamation mark (!), is a quantity defined for all integer greater than or equal to 0.
For an integer n greater than or equal to 1, the factorial is the product of all integers less than or equal to n but greater than or equal to 1. The factorial value of 0 is defined as equal to 1. The factorial values for negative integers are not defined.
Mathematically, the formula for the factorial is as follows. If n is an integer greater than or equal to 1, then
n != n(n – 1)(n – 2)(n – 3)...(3)(2)(1)
If n= 0, then n! = 1 by convention.
The factorial is of interest to number theorists. It often arises in probability calculations, particularly those involving combinations and permutations. The factorial also arises occasionally in calculus and physics.
When you factorize a number, you are multiplying that number by each consecutive number minus one.
If your number is 5, you would have:
5! = 5 * 4 * 3 * 2 * 1
The pattern would be:
0! = 1
1! = 1
2! = 2 * 1
3! = 3 * 2 * 1
4! = 4 * 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1
1. Factorialize a Number With Recursion
If the number is less than 0, reject it.
if (num < 0)
return -1;
If the number is 0, its factorial is 1.
else if (num == 0)
return 1;
Otherwise, call the recursive procedure again
else
return (num * factorialize(num - 1));
First Part of the recursion method
You need to remember that you won’t have just one call, you’ll have several nested calls
1st call – factorialize(5) will return 5 * factorialize(5 – 1) // factorialize(4)
2nd call – factorialize(4) will return 4 * factorialize(4 – 1) // factorialize(3)
3rd call – factorialize(3) will return 3 * factorialize(3 – 1) // factorialize(2)
4th call – factorialize(2) will return 2 * factorialize(2 – 1) // factorialize(1)
5th call – factorialize(1) will return 1 * factorialize(1 – 1) // factorialize(0)
Second part of the recursion method
After 5th call, method hits the if condition, it returns 1, and the function will exit with the total value
5th call will return (5 * (5 – 1)) // num = 5 * 4
4th call will return (20 * (4 – 1)) // num = 20 * 3
3rd call will return (60 * (3 – 1)) // num = 60 * 2
2nd call will return (120 * (2 – 1)) // num = 120 * 1
1st call will return (120) // num = 120
If we sum up all the calls in one line, we have
(5 * (5 – 1) * (4 – 1) * (3 – 1) * (2 – 1)) = 5 * 4 * 3 * 2 * 1 = 120
Here’s you can find the full source code:
function factorialize(num) {
if (num < 0)
return -1;
else if (num == 0)
return 1;
else {
return (num * factorialize(num - 1));
}
}
factorialize(5);
2. Factorialize a Number with a WHILE loop
Step 1. Create a variable result to store num
var result = num;
If num = 0 or num = 1, the factorial will return 1
if (num === 0 || num === 1)
return 1;
Step 2. Create the WHILE loop and decrement num by 1 at each iteration
while (num > 1) {
num–-;
result = result * num;
}
And the iteration table will look look this:
num num– var result result *= num
1st iteration: 5 4 5 20 = 5 * 4
2nd iteration: 4 3 20 60 = 20 * 3
3rd iteration: 3 2 60 120 = 60 * 2
4th iteration: 2 1 120 120 = 120 * 1
5th iteration: 1 0 120
End of the WHILE loop
Step 3. Return the factorial of the provided integer
return result;
Here’s you can find the full source code:
function factorialize(num) {
var result = num;
if (num === 0 || num === 1)
return 1;
while (num > 1) {
num--;
result = result * num;
}
return result;
}
factorialize(5);
3. Factorialize a Number with a FOR loop
If num = 0 or num = 1, the factorial will return 1
if (num == 0 || num == 1)
return 1;
We start the FOR loop and decrement i after each iteration.
for (var i = num - 1; i >= 1; i--)
num *= i;
We store the value of num at each iteration.
num num – 1 num *= i i– i >= 1?
1st iteration: 5 4 = 5 – 1 20 = 5 * 4 3 yes
2nd iteration: 20 3 = 4 – 1 60 = 20 * 3 2 yes
3rd iteration: 60 2 = 3 – 1 120 = 60 * 2 1 yes
4th iteration: 120 1 = 2 – 1 120 = 120 * 1 0 no
5th iteration: 120 0 120
End of the FOR loop
return num;
Here’s you can find the full source code:
function factorialize(num) {
if (num === 0 || num === 1)
return 1;
for (var i = num - 1; i >= 1; i--) {
num *= i;
}
return num;
}
factorialize(5);
Conclusion
I propose several solutions and explain step-by-step what happens under the hood. I hope you found this helpful.
Please Subscribe to our TruCatchBlog to get latest Post like this.
Sources
- https://www.geeksforgeeks.org/factorial-of-a-number-using-javascript/
- https://www.freecodecamp.org/news/how-to-factorialize-a-number-in-javascript-9263c89a4b38/
- https://en.wikipedia.org/wiki/Recursion_(computer_science)
- https://www.wikihow.com/Calculate-Probability
Two Best Way to Check for Palindrome in JavaScript | TryCatchBlog
[…] Step 2. Use the same chaining methods with built-in functions from the previous article ‘Three Ways to Reverse a String in JavaScript’. […]