N Factorial (N!) Calculator A Science Program by Jay Tanner - 2024 |
This program computes the EXACT value for any non-negative integer argument from 0 to 9999. The EXACT value of 0! (factorial) is shown below and consists of 1 digit.
Because factorials can become extremely large very quickly, the program is limited to 9999 as the maximum argument value because, depending on the server computer, it could take up to several seconds to compute very large factorial values and the program could possibly time-out if taking too long to complete a computation.
The following PHP function is used as the foundation for the above program. It computes the full, arbitrary-precision value of the factorial of any given non-negative integer argument. A factorial is a count of permutations (unique linear sequences), in which case, the order of the items matters. The factorial of N tells us the total count of the unique ways that N distinct objects can be arranged in a row or linear sequence. For example, 4 people can be arranged to stand in a line 4! = 24 different ways or there are 24 possible permutations (unique linear arrangements) of the 4 people. NOTE: This function should be used with caution, since it can result in extremely large factorial values. For example, even though the function could handle arguments greater than 9999, the factorial of 9999 equates to a 35656-digit number. It may take up to several seconds to compute extremely large factorial values with the possibility of a time-out if too extreme. /* ======================================================================== This function computes the arbitrary-precision factorial of a positive integer argument. Author : Jay Tanner 2014 Language : PHP v5.4.7 ------------------------------------------------------- Input argument must be a positive integer. Any decimal value will be truncated to an integer value without rounding, but negative values are invalid and will return FALSE. ERRORS: FALSE is returned on error if N is non-numeric or N < 0 ======================================================================== */ function bcFactorial ($PosIntN='0') { $N = trim($PosIntN); $F = FALSE; if (is_numeric($N) and $N >= 0) {$F=1; for($n=1; $n <= $N; $n++) {$F = bcmul($F, $n);}} return $F; }
So, if the factorial of non-negative integer N simply equates to the product all of the integers from 1 up to N multiplied together, then why does the factorial of 0 (zero) equate to 1?
The reason zero factorial equates to 1 is essentially due to its physical definition. Physically, the value of the factorial represents a count of the number of distinct ways that N distinct entities can be arranged in a row. There is only 1 physical way that 0 (zero) entities can be arranged in a row. Even though it equates to an empty set, and essentially a moot point, it still counts as one possible arrangement. Hence, by definition, zero factorial (0!) equates to only 1 possible permutation (sequence) of zero distinct objects exists and it amounts to an empty or null set of nothing at all, like an empty box. A PHP Science Program by Jay Tanner - 2024
|