- 508
- 10
- Joined
- Nov 18, 2007
This should work, yes, SHOULD, i didn't take time to proofreadOriginally Posted by ebpo
I can't remember C# (or Java for that matters), but let me take a shot at it in PHP, all you got to do is translate
Just to be sure, you want to know how much 5! does for example? This is how I would do it, in PHP again
function factorial($number){
$result = 1
if($number < 0){ // Only works with positive number;
$result = -1; // Use -1 to display an error message or something
}elseif($number == 0){
$result = 1;
}else{
for($i = $number; $i < 1; $i--){ // We will stop decreasing at 1, because we already included the *1 on the first occurence
$result = $result * $i;
}
}
return $result;
}
Another way which would b a little simpler:
Function factorial(number){
loop until number = 1{ //Create a loop that runs until the number equals 1 since you do not need to multiply by 1
((number) * (number--)) == baseresult
baseresult + result == result
}
return(result)
I know its not properly designed and none of the variables declared but u should get the jist of the algorithm