robert44444uk has asked for the wisdom of the Perl Monks concerning the following question:
Dear Pearl Monks. I am soorry to trouble you with a trifle, but I am struggling with nested loops and if statements. First I shall describe what i am hoping to achieve: A covering set is a set of primes with relationship ymodp in relation to the first member of a range of integers, such that all integers in the range are composite with factors provided by one of more of the primes in the covering set. To get to the most efficient cover, a greedy algorithm might start with small primes and take each in turn on a range of integers, and determining for each prime, the initial relationship ymodp that eliminates the most integers. The next smallest prime is then applied against this reduced set, cycling through the possibilities of ymodx, and the resulting set that has the least members forme the basis for the next prime. Often there will be a tie, with more than one y providing equally small sets, where a simple rule will be to take forward the first one found. Hence for a range of 210 integers, 2 will eliminate 105 members if the relationshpi to the first member is 1mod2 or 0mod2. Take 0mod 2 result as it was found first. 3, the next smallest prime will reduce the set to 70 whether 0mod3, 1mod3, 2mod3. Take 0mod3 as it was fonud first. All 5 mod positions for the prime 5 produce 56 remaining members for 0mod2, 0mod3, and 7 similarly leaves 48. Only at 11 do we get a difference, where the results range from 42 to 45 according to x value in the xmod11. The best of these was 42 from 5 mod11. Taking this best set forward, 12mod13 produces the samllest set, with 36 members. Etc. So I need loops for each prime and for each ymodx, and an if statement if an ymodx produces a better result for that prime, and an output that shows the best reults at each prime and the set taken forward. When I tried to do this, I am OK up to a point, and then I am finding that the program does not choose the lowest ymodx but instead ingores that, and the results after that are gibberish. So I would be grateful for guidance on how to manage these for loops and if statements to produce a result as described above.
#!/usr/bin/env perl use warnings; use strict; use feature ':5.10'; my @primes = (2,3,5,7,11,13,17) ; my @startset = (0..209); my @bestatstartofprime = @startset; my @bestincurrentprime = @startset; foreach my $x (@primes) { @bestatstartofprime = @bestincurrentprime; say "1"," ",$x," ",scalar @bestatstartofprime, " ",scalar @bestinc +urrentprime; my @mods = (0..($x-1)); foreach my $y (@mods) { say "2"," ",$x," ",$y," ",scalar @bestatstartofprime," ",s +calar @bestincurrentprime ; my @cleared = grep { ($_+$y) % $x ne 0} @bestatstartofprim +e; say "3", " ", $x," ",$y," ",scalar @bestatstartofprime," " +,scalar @bestincurrentprime," ",scalar @cleared; if (scalar @cleared lt scalar @bestincurrentprime) { say "4 loop a"," ", $x," ",$y," ",scalar @bestatstarto +fprime, " ", scalar @bestincurrentprime," ",scalar @cleared; @bestincurrentprime = @cleared; say "4 loop b"," ", $x," ",$y," ",scalar @bestatstarto +fprime, " ", scalar @bestincurrentprime," ",scalar @cleared; } say "5", " ", $x," ",$y," ",scalar @bestatstartofprime," " +,scalar @bestincurrentprime; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Nested loops and if
by poj (Abbot) on Feb 20, 2016 at 20:45 UTC | |
by robert44444uk (Acolyte) on Feb 21, 2016 at 18:40 UTC | |
by poj (Abbot) on Feb 21, 2016 at 19:03 UTC | |
by robert44444uk (Acolyte) on Feb 21, 2016 at 22:15 UTC | |
by robert44444uk (Acolyte) on Feb 21, 2016 at 08:16 UTC |