Re: algorithm help ?

From: Date: Sat, 30 Dec 2000 13:10:50 +0000
Subject: Re: algorithm help ?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-32318@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..... > > I have looked around a bit and I believe the way it should be done is > in the Linear Reduction method ? ( if this means anything). I haven't > had any luck as I'm not that hot when it comes to mathematics We have few steps to perform. 1. Selecting 19 numbers from 49 numbers: is this done or manually or randomly or on some other criteria? In fact, I wonder why this comes to the pictuer at all. It would be another combination problem then. 2. Generating permutations 3. Generating combinations I will address the step 2 ... Let's assume that the numbers are in an array $n[1] ... $n[19] as selected in step 1. Let's have another array $used[1] ... $used[19] which is of type boolean and initialized to false. We need yet another array $perm[1] ... $perm[19] to hold the current permutation. The following piece should generate the permutations. It is very straightforward and doesn't contain any optimizations. IMPORTANT: The generated output is very large, and it takes a LOT of server process time, so be careful! <? $max = 19; function print_permutation() { global $perm; global $max; for ($i = 1; $i <= $max; $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; if ($level > $max) { 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 the time being we set $n to {1...19} for ($i = 1; $i <= $max; $i++){ $n[$i] = $i; $used[$i] = false; } generate_permutations(1); ?>

« previous php.general (#32318) next »