- #1
Jay1
- 14
- 0
The general integer factorial can be expressed by the equation:
$$N! = \prod_{x~=~1}^N {x} = {1 \cdot 2 \cdot 3 \cdot ~~...~~ \cdot N}$$
This simply means that N-factorial is the sequential product of all integers from 1 to N.
Below is a simple PHP function to compute the arbitrary-precision factorial of any positive integer. This function should be used with caution, since small arguments can result in very large factorial values in the hundreds and even thousands of digits. For example, 250 factorial equates to this 493-digit number:
250! =
32328562609091077323208145520243684709948437176737806667479424271128237475551112
09488817915371028199450928507353189432926730931712808990822791030279071281921676
52724018926473321804118626100683292536513367893908956993571353017504051317876007
72479330654023390061648255522488194365725860573992226412548329822048491377217766
50641276858807153128978777672951913990844377478702589172973255150283241787320658
18848206247858265980884882554880000000000000000000000000000000000000000000000000
0000000000000
The function has a built-in safety limit of 9999, but this can easily be changed. However, it is important to point out that factorials of larger values can take several seconds of computation time, depending on the server speed and there is the possibility of a script time-out if the argument is too large.
Below is an example program calling the function to compute 52 factorial (52!):
The returned value should be:
The above function is useful when you want the EXACT factorial value, far beyond the reach of the standard 16-digit arithmetic, which would only return it as something like: 52! = 8.0658175170944E+67, rather than the full 68-digit value.
If you were to guess at the value of every card drawn at random without replacement from a shuffled standard deck of 52 playing cards, the odds of you guessing every card correctly would be 1 to 52 factorial, or about 1 chance in 80.658 unvigintillion. In other words, a snowball's chance in you-know-where!
Or, as Mr. Spock would say:
Exactly 1 chance out of
80 unvigintillion
658 vigintillion
175 novemdecillion
170 octodecillion
943 septendecillion
878 sexdecillion
571 quindecillion
660 quattuordecillion
636 tredecillion
856 duodecillion
403 undecillion
766 decillion
975 nonillion
289 octillion
505 septillion
440 sextillion
883 quintillion
277 quadrillion
824 trillion
of success.Here's an on-line factorial calculator built around the above function:
Factorial Calculator - PHP Science Labs
$$N! = \prod_{x~=~1}^N {x} = {1 \cdot 2 \cdot 3 \cdot ~~...~~ \cdot N}$$
This simply means that N-factorial is the sequential product of all integers from 1 to N.
Below is a simple PHP function to compute the arbitrary-precision factorial of any positive integer. This function should be used with caution, since small arguments can result in very large factorial values in the hundreds and even thousands of digits. For example, 250 factorial equates to this 493-digit number:
250! =
32328562609091077323208145520243684709948437176737806667479424271128237475551112
09488817915371028199450928507353189432926730931712808990822791030279071281921676
52724018926473321804118626100683292536513367893908956993571353017504051317876007
72479330654023390061648255522488194365725860573992226412548329822048491377217766
50641276858807153128978777672951913990844377478702589172973255150283241787320658
18848206247858265980884882554880000000000000000000000000000000000000000000000000
0000000000000
The function has a built-in safety limit of 9999, but this can easily be changed. However, it is important to point out that factorials of larger values can take several seconds of computation time, depending on the server speed and there is the possibility of a script time-out if the argument is too large.
Code:
function bcN_Factorial ($N=0)
{
if (!is_numeric($N) or $N < 0 or $N > 9999) {return FALSE;}
$n = bcadd($N, 0);
$P=1; for ($i=1; $i <= $n; $i++) {$P = bcmul($P, $i);}
return $P;
}
Below is an example program calling the function to compute 52 factorial (52!):
Code:
<?php
$N = 52;
print "$N factorial =<br>" . bcN_Factorial ($N);
// -----------------------------
function bcN_Factorial ($N=0)
{
if (!is_numeric($N) or $N < 0 or $N > 9999) {return FALSE;}
$n = bcadd($N, 0);
$P=1; for ($i=1; $i <= $n; $i++) {$P = bcmul($P, $i);}
return $P;
}
?>
The returned value should be:
Code:
52 factorial =
80658175170943878571660636856403766975289505440883277824000000000000
The above function is useful when you want the EXACT factorial value, far beyond the reach of the standard 16-digit arithmetic, which would only return it as something like: 52! = 8.0658175170944E+67, rather than the full 68-digit value.
If you were to guess at the value of every card drawn at random without replacement from a shuffled standard deck of 52 playing cards, the odds of you guessing every card correctly would be 1 to 52 factorial, or about 1 chance in 80.658 unvigintillion. In other words, a snowball's chance in you-know-where!
Or, as Mr. Spock would say:
Exactly 1 chance out of
80 unvigintillion
658 vigintillion
175 novemdecillion
170 octodecillion
943 septendecillion
878 sexdecillion
571 quindecillion
660 quattuordecillion
636 tredecillion
856 duodecillion
403 undecillion
766 decillion
975 nonillion
289 octillion
505 septillion
440 sextillion
883 quintillion
277 quadrillion
824 trillion
of success.Here's an on-line factorial calculator built around the above function:
Factorial Calculator - PHP Science Labs
Last edited: