# Combinatorics algorithm

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

•  Subject
• Author
• Posted on
Hello!

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?

Regards,
OMykle

## Re: Combinatorics algorithm

all_my_trash@hotmail.com wrote:

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;
}
return(\$ans);
}

function Permutation(\$selectcount,\$availablecount)
{
\$ans = Factorial(\$availablecount) /
Factorial(\$availablecount-\$selectcount);
return (\$ans);
}

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

## Re: Combinatorics algorithm

Kimmo Laine wrote:

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

next_page.php

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

--