mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 07:08:13 +01:00
37 lines
730 B
PHP
37 lines
730 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
|
|
* @throws \Exception
|
|
*/
|
|
function factorial(int $number)
|
|
{
|
|
static $cache = []; //internal caching memory for speed
|
|
|
|
if ($number < 0) {
|
|
throw new \Exception("Negative numbers are not allowed for calculating Factorial");
|
|
}
|
|
|
|
// Factorial of 0 is 1
|
|
if ($number === 0) {
|
|
return 1;
|
|
}
|
|
|
|
if (isset($cache[$number])) {
|
|
return $cache[$number];
|
|
}
|
|
|
|
// Recursion since x! = x * (x-1)!
|
|
$fact = ($number * factorial($number - 1));
|
|
|
|
$cache[$number] = $fact;
|
|
|
|
return $fact;
|
|
}
|