Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2008
    Location
    Jupiter
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Factorial function

    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 Code:
    <?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

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    PHP Code:
    <?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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    tokio (05-11-2009)

  • #3
    New Coder
    Join Date
    Jun 2008
    Location
    Jupiter
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    PHP Code:
    <?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?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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+fac...ient=firefox-a

    This is for 10!
    http://www.google.com/search?q=10+fa...ient=firefox-a
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    tokio (05-11-2009)

  • #5
    New Coder
    Join Date
    Jun 2008
    Location
    Jupiter
    Posts
    49
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    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+fac...ient=firefox-a

    This is for 10!
    http://www.google.com/search?q=10+fa...ient=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.

  • #6
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    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 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
    Last edited by funnymoney; 05-11-2009 at 02:51 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •