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; } }

In reply to Nested loops and if by robert44444uk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.