Help with mathmatical custom function

Discussion in 'PHP' started by greenstar91, May 21, 2008.

  1. #1
    function factorial($number) {
            if ($number == 0) return 1;
            return $number * factorial($number - 1);
    }
    PHP:
    On the internet I have found this function which states the factorial of a number.

    I now want to make a function where basically there will be a user inputted number say x and a power y, but then i want to add x^y to x^y-1 ... x^y-y+1 where y-y+1 is one.

    This could be used in non-repetitive combinations for example where for a 4 length combination with 10 possible values for each digit would be 10^4 + 10^3 + 10^2 + 10^1 = 10000 + 1000 + 100 + 10 = 11110

    Thanks
     
    greenstar91, May 21, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    <?php
    function combination($number, $number2) {
            if ($number == 0) return;
            return pow($number2, $number) + combination($number - 1, $number2);
    }
    echo combination(4, 10);
    ?>
    
    PHP:
    Output: 11110

    It should guide you to where you want to go. Rep is appreciated ;)

    Jay
     
    jayshah, May 21, 2008 IP
  3. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #3
    function factorial($number,$number2) { if ($number == 0) return 1;
    else
    {
    $sum=0;
    for ($i=$number2;$i>=1;$i--)
    {
    $sum=pow($number,$i)+sum;
    }
    return $sum;
    }

    }
     
    kmap, May 21, 2008 IP