note 100153 added to function.array-product
| From: | marcel dot glacki at stud dot fh-swf dot de | Date: | Tue, 28 Sep 2010 15:36:54 +0000 |
| Subject: | note 100153 added to function.array-product | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172768@lists.php.net to get a copy of this message | ||
You can use array_product to calculate the factorial of n:
<?php
function factorial( $n )
{
if( $n < 1 ) $n = 1;
return array_product( range( 1, $n ));
}
?>
In addition to mattyfroese (see below), if you need the factorial without having array_product
available, here is one:
<?php
function factorial( $n )
{
if( $n < 1 ) $n = 1;
for( $p++; $n; ) $p *= $n--;
return $p;
}
?>
----
Server IP: 217.160.235.204
Probable Submitter: 94.134.187.44
----
Manual Page -- http://www.php.net/manual/en/function.array-product.php
Edit -- https://master.php.net/note/edit/100153
Del: integrated -- https://master.php.net/note/delete/100153/integrated
Del: useless -- https://master.php.net/note/delete/100153/useless
Del: bad code -- https://master.php.net/note/delete/100153/bad+code
Del: spam -- https://master.php.net/note/delete/100153/spam
Del: non-english -- https://master.php.net/note/delete/100153/non-english
Del: in docs -- https://master.php.net/note/delete/100153/in+docs
Del: other reasons-- https://master.php.net/note/delete/100153
Reject -- https://master.php.net/note/reject/100153
Search -- https://master.php.net/manage/user-notes.php