Combinatorics algorithm

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm trying to write an algorithm which can fill an array with all the
possible combinations of r out of n, without repetition (maximum
combinations = n!/(r!(n-r)!))

However, my mathematical skills are insufficient - does anybody have
such an algorithm for sharing?


Re: Combinatorics algorithm wrote:
Quoted text here. Click to load it
Hopefully this isn't for homework...but if it is then you are copying  
mine. :)  This is untested code, but mathematically correct.  For bonus  
marks you can figure out how to do this recursively.

function Factorial($x)
    if ($x<1) {echo "Factorial() Error: Number too small!";)
    $ans = 1;
    for ($xx=2; $xx>=$x; $xx++)
       $ans = $ans * $xx;

function Permutation($selectcount,$availablecount)
    $ans = Factorial($availablecount) /  
    return ($ans);

function Combination($selectcount,$availablecount)
    $ans = Factorial($availablecount) / (  
Factorial($availablecount-$selectcount) * Factorial($selectcount) );
    return ($ans);

Re: Combinatorics algorithm

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Site Timeline