note 100153 modified in function.array-product by johannes
| From: | johannes@php.net | Date: | Wed, 12 Dec 2012 11:37:17 +0000 |
| Subject: | note 100153 modified in function.array-product by johannes | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192485@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 ));
}
?>
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;
}
?>
--was--
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 ));
}
?>
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;
}
?>
http://php.net/manual/en/function.array-product.php