note 50149 added to ref.math
| From: | tmpa at yahoo dot com | Date: | Sat, 19 Feb 2005 20:42:39 +0000 |
| Subject: | note 50149 added to ref.math | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85251@lists.php.net to get a copy of this message | ||
while joogat's one line function is short, it is probably better to calculate factorial
iteratively instead of recursively. keep in mind if you want large factorials, you'll need to
use some sort of arbitrary precision integer or perhaps the BCMath functions. then again, unless
you're trying to do large numbers (170! is the highest that you can do that does not return
infinity) you probably won't notice any time difference.
<?php
function factorial($in) {
// 0! = 1! = 1
$out = 1;
// Only if $in is >= 2
for ($i = 2; $i <= $in; $i++) {
$out *= $i;
}
return $out;
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.math.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50149
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50149&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50149&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50149&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50149&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50149&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50149&report=yes
Search -- http://master.php.net/manage/user-notes.php