For the record, although developed independently my version at Re: Split range 0 to M into N non-overlapping (roughly equal) ranges. has the same sort of bug, triggered by at least one of the same input pairs. I guess when two people hit the same flaw, we at least know we're close to the right solution.

Since the fix is already discussed for JavaFan's map version, I'm sure the translation of that fix to the for loop can be left as an exercise. Personally, my fix would probably be to pull in bignum, which works well since it's a rounding error.

Broken:

[chris@pineapple ranges-892828]$ perl ranges -M=15 -N=12 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 3 ( 2) 3 : from 4 to 4 ( 1) 4 : from 5 to 5 ( 1) 5 : from 6 to 6 ( 1) 6 : from 7 to 8 ( 2) 7 : from 9 to 9 ( 1) 8 : from 10 to 11 ( 2) 9 : from 12 to 12 ( 1) 10 : from 13 to 13 ( 1) 11 : from 14 to 15 ( 2) [chris@pineapple ranges-892828]$ perl ranges -M=15 -N=11 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 3 ( 2) 3 : from 4 to 4 ( 1) 4 : from 5 to 6 ( 2) 5 : from 7 to 7 ( 1) 6 : from 8 to 9 ( 2) 7 : from 10 to 10 ( 1) 8 : from 11 to 12 ( 2) 9 : from 13 to 13 ( 1) 10 : from 14 to 15 ( 2) [chris@pineapple ranges-892828]$ perl ranges -M=14 -N=10 0 : from 0 to 0 ( 1) 1 : from 1 to 2 ( 2) 2 : from 3 to 3 ( 1) 3 : from 4 to 5 ( 2) 4 : from 6 to 6 ( 1) 5 : from 7 to 8 ( 2) 6 : from 9 to 9 ( 1) 7 : from 10 to 11 ( 2) 8 : from 12 to 12 ( 1) 9 : from 13 to 14 ( 2) [chris@pineapple ranges-892828]$ perl ranges -M=14 -N=11 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 3 ( 2) 3 : from 4 to 4 ( 1) 4 : from 5 to 5 ( 1) 5 : from 6 to 7 ( 2) 6 : from 8 to 8 ( 1) 7 : from 9 to 9 ( 1) 8 : from 10 to 11 ( 2) 9 : from 12 to 12 ( 1) 10 : from 13 to 13 ( 1) [chris@pineapple ranges-892828]$ perl ranges -M=14 -N=12 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 2 ( 1) 3 : from 3 to 4 ( 2) 4 : from 5 to 5 ( 1) 5 : from 6 to 6 ( 1) 6 : from 7 to 7 ( 1) 7 : from 8 to 9 ( 2) 8 : from 10 to 10 ( 1) 9 : from 11 to 11 ( 1) 10 : from 12 to 12 ( 1) 11 : from 13 to 14 ( 2) [chris@pineapple ranges-892828]$ perl ranges -M=13 -N=10 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 3 ( 2) 3 : from 4 to 4 ( 1) 4 : from 5 to 6 ( 2) 5 : from 7 to 7 ( 1) 6 : from 8 to 8 ( 1) 7 : from 9 to 10 ( 2) 8 : from 11 to 11 ( 1) 9 : from 12 to 13 ( 2) [chris@pineapple ranges-892828]$ perl ranges -M=13 -N=11 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 2 ( 1) 3 : from 3 to 4 ( 2) 4 : from 5 to 5 ( 1) 5 : from 6 to 6 ( 1) 6 : from 7 to 7 ( 1) 7 : from 8 to 9 ( 2) 8 : from 10 to 10 ( 1) 9 : from 11 to 11 ( 1) 10 : from 12 to 13 ( 2) [chris@pineapple ranges-892828]$ perl ranges -M=13 -N=12 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 2 ( 1) 3 : from 3 to 3 ( 1) 4 : from 4 to 4 ( 1) 5 : from 5 to 6 ( 2) 6 : from 7 to 7 ( 1) 7 : from 8 to 8 ( 1) 8 : from 9 to 9 ( 1) 9 : from 10 to 10 ( 1) 10 : from 11 to 11 ( 1) 11 : from 12 to 12 ( 1)

Fixed:

[chris@pineapple ranges-892828]$ perl -Mbignum ranges -M=14 -N=11 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 3 ( 2) 3 : from 4 to 4 ( 1) 4 : from 5 to 5 ( 1) 5 : from 6 to 7 ( 2) 6 : from 8 to 8 ( 1) 7 : from 9 to 9 ( 1) 8 : from 10 to 11 ( 2) 9 : from 12 to 12 ( 1) 10 : from 13 to 14 ( 2) [chris@pineapple ranges-892828]$ perl -Mbignum ranges -M=13 -N=12 0 : from 0 to 0 ( 1) 1 : from 1 to 1 ( 1) 2 : from 2 to 2 ( 1) 3 : from 3 to 3 ( 1) 4 : from 4 to 4 ( 1) 5 : from 5 to 6 ( 2) 6 : from 7 to 7 ( 1) 7 : from 8 to 8 ( 1) 8 : from 9 to 9 ( 1) 9 : from 10 to 10 ( 1) 10 : from 11 to 11 ( 1) 11 : from 12 to 13 ( 2)

It's maybe slower than fudging, but I have more faith in it. It's easy enough to add a line at the top of the program to let someone else deal with the intricacies since this isn't going to be the resource-hungry part of your code anyway.


In reply to Re^4: Split range 0 to M into N non-overlapping (roughly equal) ranges. by mr_mischief
in thread Split range 0 to M into N non-overlapping (roughly equal) ranges. by BrowserUk

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.