/*
   ###########################################################################
   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.

   If the number is very large, the program will simply go on and on trying
   to calculate the factorial and the script may eventually time-out.

   ARGUMENT
   IntN         Integer value N
                Range 0 to 9999, unless the safety limit is changed.

   RETURNS:
   Factorial of integer argument N (N!)

   ERRORS:
   Returns FALSE if non-numeric argument or outside of valid range.

   NO DEPENDENCIES
   ###########################################################################
*/

   function BC_Fact ($IntN='0')
{

// Read input integer string argument
// ERROR if non-numeric string value.
   $N = trim($IntN);  if (!Is_Numeric($N)) {return FALSE;}

// Truncate argument N (if non-integer value).
   $N = bcAdd($IntN, '0');

// ERROR if argument N is ouside valid range.
   if ($N < 0 or $N > 9999) {return FALSE;}

// Execute loop to compute factorial product (F = N!).
   $F = 1;  for ($i=1;  $i <= $N;  $i++) {$F = bcMul($F, $i);}

// Done.
   return $F;

} // End of  BC_Fact(...)