This is an old revision of the document!
Back to: Arbitrary-Precision Math
INDEX
This function computes the factorial of any positive integer N. This function should be used with caution because the values of factorials can rapidly grow very, very large. For this reason, the function has a built-in limit of N=9999. This limit can be removed, but it is not recommended.
Mathematically, the factorial of integer N is simply the product of all the sequential integers from 1 to N multiplied together. Zero factorial equates to 1, by definition. Physically, N factorial represents a count of the total number of possible unique linear sequences (permutations) we can make using N distinct items.
Factorial Calculator based on the function defined below.
With factorial permutations, the only thing that matters is the sequential order of the items. For example, there are 6 unique permutations of 3 people, which is to say that there are (3 x 2 x 1) = 6 ways that 3 people can be lined up in a row. There are (5 x 4 x 3 x 2 x 1) = 120 permutations of 5 people or 120 possible different sequences in which 5 people can be lined up in a row, etc.
Large values can take several seconds to compute in arbitrary-precision, depending on the computer, because they are exact values and could extend out to tens of thousands of digits in some cases. For example, 100 factorial equates to a 158-digit integer and at the limit of 9999 factorial, its value equates to a 35656-digit integer!
Beyond the set limit, there is a possibility of a script-timeout error or an otherwise prohibitively long wait for the result. Different web servers on which you may run a program may apply different script-time-out limits (typically 30 seconds, but it can usually be extended).
/* =========================================================================== This function returns the arbitrary-precision factorial of any non-negative integer value. This function should be used with caution, since factorial values can be extremely large for relatively small arguments. It may take several seconds to compute extremely large factorials (e.g. > 10000!), so a safety limit of 9999 is built in, but it can be easily removed. Input argument must be a positive integer. Any decimal argument will be silently truncated to an integer, without rounding, rather than causing an error. ERRORS: FALSE is returned on error, if argument is non-numeric or negative or argument exceeds maximum set limit (default = 9999). =========================================================================== */ function bcN_Factorial ($N=0) { // Error if argument N is non-numeric, negative or > 9999. if (!is_numeric($N)) {return FALSE;} // Truncate any decimal argument to an // integer value, without rounding. $n = bcadd($N, 0); // Error if argument outside valid range (0 to 9999). if ($n < 0 or $n > 9999) {return FALSE;} // Execute loop to compute N factorial product (P). $P=1; for ($i=1; $i <= $n; $i++) {$P = bcmul($P, $i);} // Done. return $P; }
Running the following example program produces the table of factorials from 1 to 64 that follows.
<?php /* This program demonstrates the computation of exact integer factorial values using arbitrary-precision arithmetic. AUTHOR: Jay Tanner */ // ------------------------------------------------------ // Compute table of EXACT integer factorials from 1 to N! $N = 64; $FactorialTable = ""; for($n=1; $n <= $N; $n++) { $F=1; for ($i=1; $i <= $n; $i++) {$F = bcmul($F, $i);} $FactorialTable .= substr($n . str_repeat(" ", 4), 0, 4) . "$F\n"; } // Print out the computed table. print "<b><pre>\nN N!\n$FactorialTable</pre></b>\n"; ?>
N N!
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
11 39916800
12 479001600
13 6227020800
14 87178291200
15 1307674368000
16 20922789888000
17 355687428096000
18 6402373705728000
19 121645100408832000
20 2432902008176640000
21 51090942171709440000
22 1124000727777607680000
23 25852016738884976640000
24 620448401733239439360000
25 15511210043330985984000000
26 403291461126605635584000000
27 10888869450418352160768000000
28 304888344611713860501504000000
29 8841761993739701954543616000000
30 265252859812191058636308480000000
31 8222838654177922817725562880000000
32 263130836933693530167218012160000000
33 8683317618811886495518194401280000000
34 295232799039604140847618609643520000000
35 10333147966386144929666651337523200000000
36 371993326789901217467999448150835200000000
37 13763753091226345046315979581580902400000000
38 523022617466601111760007224100074291200000000
39 20397882081197443358640281739902897356800000000
40 815915283247897734345611269596115894272000000000
41 33452526613163807108170062053440751665152000000000
42 1405006117752879898543142606244511569936384000000000
43 60415263063373835637355132068513997507264512000000000
44 2658271574788448768043625811014615890319638528000000000
45 119622220865480194561963161495657715064383733760000000000
46 5502622159812088949850305428800254892961651752960000000000
47 258623241511168180642964355153611979969197632389120000000000
48 12413915592536072670862289047373375038521486354677760000000000
49 608281864034267560872252163321295376887552831379210240000000000
50 30414093201713378043612608166064768844377641568960512000000000000
51 1551118753287382280224243016469303211063259720016986112000000000000
52 80658175170943878571660636856403766975289505440883277824000000000000
53 4274883284060025564298013753389399649690343788366813724672000000000000
54 230843697339241380472092742683027581083278564571807941132288000000000000
55 12696403353658275925965100847566516959580321051449436762275840000000000000
56 710998587804863451854045647463724949736497978881168458687447040000000000000
57 40526919504877216755680601905432322134980384796226602145184481280000000000000
58 2350561331282878571829474910515074683828862318181142924420699914240000000000000
59 138683118545689835737939019720389406345902876772687432540821294940160000000000000
60 8320987112741390144276341183223364380754172606361245952449277696409600000000000000
61 507580213877224798800856812176625227226004528988036003099405939480985600000000000000
62 31469973260387937525653122354950764088012280797258232192163168247821107200000000000000
63 1982608315404440064116146708361898137544773690227268628106279599612729753600000000000000
64 126886932185884164103433389335161480802865516174545192198801894375214704230400000000000000
By examining the above table, one can see how rapidly the values of factorials increase.