note 100153 modified in function.array-product by danbrown

From: Date: Tue, 28 Sep 2010 15:39:46 +0000
Subject: note 100153 modified in function.array-product by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172770@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 )); } ?> 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; } ?> http://php.net/manual/en/function.array-product.php

« previous php.notes (#172770) next »