Re: algorithm help ?
| From: | Anuradha Ratnaweera | Date: | Sat, 30 Dec 2000 15:29:44 +0000 |
| Subject: | Re: algorithm help ? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-32317@lists.php.net to get a copy of this message | ||
On Fri, 29 Dec 2000, Gianni Ponzi wrote:
> this is what i'd like to do....
>
> I'd like to be able to select 19 numbers between 1 and 49 and work out
> the permutations / combinations of those numbers in groups of six.
>
> eg 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
>
> results:
>
> 1 2 3 4 5 6
> 1 2 3 5 6 7
> 1 2 3 6 7 8
>
>
> and so on.....
>
My previous solutions is for generating permutations of length 19. But
what is wanted is permutations of length 6. So here is the corrected
program.
Anuradha
<?
$max = 19;
$perm_size = 6;
function print_permutation()
{
global $perm;
global $max;
global $perm_size;
for ($i = 1; $i <= $perm_size; $i++) {
echo "$perm[$i] ";
// note that this puts an unnecessary space at the end
}
echo "<br>";
}
function generate_permutations($level)
{
global $perm;
global $n;
global $used;
global $max;
global $perm_size;
if ($level > $perm_size) {
print_permutation();
return;
}
for ($i = 1; $i <= $max; $i++) {
if (!$used[$i]) {
$used[$i] = true;
$perm[$level] = $n[$i];
generate_permutations($level + 1);
$used[$i] = false;
}
}
}
for ($i = 1; $i <= $max; $i++){
$n[$i] = $i;
$used[$i] = false;
}
generate_permutations(1);
?>