Search code examples
phpfactorial

Factorial of 170+


everytime I try to get the factorial of 171, I get INF. 170 works fine. Is it possible to get the factorial of 171+ in a script? How? My function:

function factorial($n) {
    if ($n == 0) return 1;
    return $n * factorial($n - 1);
}

Solution

  • You'll have to use BC Math or GNU MP extension. PHP doesn't provide any tools for high-values or high-precision operations OOTB.