mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-07-12 04:26:20 +02:00
21 lines
510 B
PHP
21 lines
510 B
PHP
<?php
|
|
/**
|
|
* This function calculates
|
|
* and returns the factorial
|
|
* of provided positive integer
|
|
* number.
|
|
*
|
|
* @param Integer $number Integer input
|
|
* @return Integer Factorial of the input
|
|
*/
|
|
function factorial(int $number)
|
|
{
|
|
if ($number < 0) {
|
|
throw new \Exception("Negative numbers are not allowed for calculating Factorial");
|
|
}
|
|
if ($number === 0) {
|
|
return 1; // Factorial of 0 is 1
|
|
}
|
|
return ($number * factorial($number - 1)); // Recursion since x! = x * (x-1)!
|
|
}
|