View Full Version : Factorial function

tokio

05-11-2009, 06:52 AM

I know php have their built in mathematical functions, but im trying to write my own mathematical function so i can learn the basics for php. What im trying to do is write a mathematical function so that it will work for all real positive integers. The factorial operator is denoted with (!). An example is 3! which equals 3*2*1. Here is what i came up with thus far:

<?php

function factorial($n) {

return (($n*($n-1)*($n-2)));

}

$summation = factorial(3);

print $summation;

?>

This obviously works for 3!, but its not true for all real positive integers. I would like to make a function that allows me to substitute any number for $n and then outputting the correct factorial value.

Tokio

_Aerospace_Eng_

05-11-2009, 07:46 AM

<?php

function factorial($n) {

if ($n == 0) return 1;

return $n * factorial($n - 1);

}

$summation = factorial(3);

print $summation;

?>

You need to make it recursive.

tokio

05-11-2009, 08:57 AM

<?php

function factorial($n) {

if ($n == 0) return 1;

return $n * factorial($n - 1);

}

$summation = factorial(3);

print $summation;

?>

You need to make it recursive.

Sup Aerospace, and thinks for the int man. But, i think its an error in the code, and i couldn't depict any fallacies. Did the code worked when you executed it?

_Aerospace_Eng_

05-11-2009, 09:01 AM

I just tested it and it worked fine. I put in 3 and got 6, then I put in 10 and got 3628800

If you notice in these google results that its correct.

This is for 3!

http://www.google.com/search?q=3+factorial&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

This is for 10!

http://www.google.com/search?q=10+factorial&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

tokio

05-11-2009, 09:14 AM

I just tested it and it worked fine. I put in 3 and got 6, then I put in 10 and got 3628800

If you notice in these google results that its correct.

This is for 3!

http://www.google.com/search?q=3+factorial&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

This is for 10!

http://www.google.com/search?q=10+factorial&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

Ok, i copied and pasted the code to a blank file on my server and it works. I originally pasted it into a file that i had previous code that appeared to work fine, but i must of had some errors that i couldn't find.

funnymoney

05-11-2009, 03:49 PM

Just a while ago had similar need..

Except i had to get all factorials till given number, so here if someone needs the code

<?php

function factiorial($n) {

$result = "";

$m=1;

while ($i <= $n) {

++$i; $result .= "For $i!"; $m *= $i; $result .= " result is $m <br>";

}

return $result;

}

$result = factiorial(5);

print $result;

?>

A bit messy, but does the trick :\

Max, 170

steban

01-23-2017, 10:05 AM

this another solution on how to solve factorial/function on youtube vodeo https://youtu.be/9A6D8-_fKNk

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.