PHP Algorithms – factorial recursive solution
<?php
/*
* EN factorial
*/
function fact($n){
if($n < 1) return 1;
$arr[$n] = $n * fact($n - 1);
return $n * fact($n - 1); // 4 * 3 * 2 * 1
}
echo fact(4); // 24
PHP Algorithms – factorial none recursive solution
<?php
/*
* EN(factorial)
*/
function powf($n){
$sum = $n;
while($n - 1 > 0){
$n--;
$sum *= $n;
}
return $sum;
}
echo powf(4); // 24
<?php
/*
* EN(factorial)
*/
function powf($n){
$sum = $n;
for($i = $n; $i - 1 > 0; $i--){
$n--;
$sum *= $n;
}
return $sum;
}
echo powf(4); // 24