* It must also not be ridiculously large: * * <code> * php > var_dump(factorial(1e100)); * PHP Fatal error: Uncaught exception 'OverflowException' with message * '$n too large' * </code> */ function factorial($n) { if (!($n >= 0)) { throw new InvalidArgumentException('$n must be >= 0'); } if (floor($n) != $n) { throw new InvalidArgumentException('$n must be exact integer'); } if ($n + 1 == $n) { throw new OverflowException('$n too large'); } $result = 1; $factor = 2; while ($factor <= $n) { $result *= $factor; $factor += 1; } return $result; } if (!count(debug_backtrace())) { require dirname(__FILE__) . '/../src/DocTest/import.php'; DocTest::testObj('factorial', null, true); }