PHP | Factorial of a number
Last Updated :
14 May, 2025
In mathematics, the factorial of a number is the product of all positive integers less than or equal to that number. It is denoted by n!. The factorial of a number can be expressed mathematically as:
n! = n * (n-1) * (n-2) * ........ * 1
Methods to Calculate Factorials in PHP
There are several methods to calculate the factorial of a number in PHP. Let’s explore some of the most common approaches.
1. Using a Loop
One of the most straightforward ways to calculate the factorial of a number is by using a loop. We will multiply all integers from 1 to the given number.
Now, let us understand with the help of the example:
PHP
<?php
function factorial($n) {
$result = 1;
for ($i = 1; $i <= $n; $i++) {
$result *= $i;
}
return $result;
}
echo "Factorial of 5 is " . factorial(5); // Output: Factorial of 5 is 120
?>
OutputFactorial of 5 is 120
In this example:
- We define a function
factorial($n)
that calculates the factorial of the number. - The loop runs from 1 to
$n
, multiplying each number with $result
to accumulate the product. - Finally, the result is returned.
2. Using Recursion
Recursion is another elegant way to calculate the factorial of a number. In this method, the function calls itself, reducing the number until it reaches the base case (0! = 1
).
Now, let us understand with the help of the example:
PHP
<?php
function factorial($n) {
if ($n == 0) {
return 1;
}
return $n * factorial($n - 1);
}
echo "Factorial of 5 is " . factorial(5); // Output: Factorial of 5 is 120
?>
OutputFactorial of 5 is 120
In this example:
- The base case for recursion is when
$n
is 0
. In that case, the factorial is 1
. - The recursive case multiplies
$n
by the factorial of n-1
. - The function keeps calling itself until the base case is reached.
3. Using PHP’s Built-In gmp_fact()
Function
If you need to calculate the factorial of very large numbers, PHP provides the gmp_fact()
function in the GMP (GNU Multiple Precision) library, which handles large integers more efficiently.
Now, let us understand with the help of the example:
PHP
<?php
$factorial = gmp_fact(5);
echo "Factorial of 5 is " . gmp_strval($factorial); // Output: Factorial of 5 is 120
?>
In this example:
- The
gmp_fact()
function calculates the factorial of the number, and gmp_strval()
converts the result into a string for easy display. - This method is highly efficient for very large numbers because it supports arbitrary-precision arithmetic.
4. Using bcMath
Functions for Large Numbers
For extremely large numbers, PHP's bcMath
library can be used to handle arbitrary-precision numbers. The bcMul()
function can be used to calculate the factorial by multiplying the numbers.
Now, let us understand with the help of the example:
PHP
<?php
function factorial($n) {
$result = '1'; // bcMath functions work with strings
for ($i = 2; $i <= $n; $i++) {
$result = bcmul($result, (string)$i); // Multiply result by i
}
return $result;
}
echo "Factorial of 5 is " . factorial(5); // Output: Factorial of 5 is 120
?>
In this example:
- The
bcmul()
function multiplies two numbers (in string format) and returns the result as a string, which helps to avoid overflow issues when dealing with large integers. - This approach is helpful when dealing with very large numbers that exceed PHP’s default integer size.
Conclusion
In this article, we explored different methods to calculate the factorial of a number in PHP. Whether you choose to use a loop, recursion, built-in functions like gmp_fact()
, or even libraries like bcMath
, the choice depends on the size of the numbers and the requirements of your application.
Similar Reads
Find the factorial of a number in pl/sql Given a number, your task to print the factorial of that number using pl/sql. Examples: Input : 5 Output : 120 Explanation: 5! = 5 * 4 * 3 * 2 * 1 = 120 Input : 4 Output : 24 Basic structure of pl/sql block declare -- declare all the variables begin -- for start block -- make a program here end -- f
1 min read
First digit in factorial of a number Given a positive integer n, find the first digit in its factorial. Examples : Input : n = 5 Output : 1 Factorial of 5 is 120 and first digit is 1. Input : 1000 Output : 4 A simple solution is to compute factorial of number, then find first digit in it. The above solution causes overflow soon. A bett
5 min read
JavaScript - Factorial of a number in JS The factorial of a non-negative integer is the product of all positive integers less than or equal to that number. Itâs denoted by ân!â where n is the integer. Here are the various methods to find the factorial of a number in JavaScript.Logicx!= 1*(x-3)*(x-2)*(x-1).......xNote:Â Factorials of negativ
3 min read
One line function for factorial of a number Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. Example : Factorial of 6 is 6 * 5 * 4 * 3 * 2 * 1 which is 720. We can find the factorial of a number in one line with the help of Ternary operator or commonly known as Conditional operator in recursio
3 min read
Numbers whose factorials end with n zeros Given an integer n, we need to find the number of positive integers whose factorial ends with n zeros.Examples: Input : n = 1 Output : 5 6 7 8 9 Explanation: Here, 5! = 120, 6! = 720, 7! = 5040, 8! = 40320 and 9! = 362880. Input : n = 2 Output : 10 11 12 13 14 Recommended PracticeN trailing zeroes i
6 min read
PHP | Sum of digits of a number This is a simple PHP program where we need to calculate the sum of all digits of a number. Examples: Input : 711 Output : 9 Input : 14785 Output : 25 In this program, we will try to accept a number in the form of a string and then iterate through the length of the string. While iterating we will ext
1 min read