BrowserUk:

Hmmm ... I can make it differently, but no cleaner, unfortunately:

$ cat 892828_a.pl #!/usr/bin/perl use strict; use warnings; my ($M, $N) = (9999997, 3); my @ranges = map { $_ * int($M/$N) } 0 .. $N-1; @ranges = map { [ $ranges[$_], $ranges[$_+1]//$M ] } 0 .. $N-1; print join(", ", map { "(".join("-",@{$_}).")" } @ranges), "\n"; $ perl 892828_a.pl (0-3333332), (3333332-6666664), (6666664-9999997)

That one gets the second value wrong by one. I tried to find a way to stack the maps together, but couldn't figure out how to do it nicely.

This one gets the values right, but uses an uglier map statement, and still requires the "$ranges[-1][1]=$M" line, which I don't particularly care for:

$ cat 892828_b.pl #!/usr/bin/perl use strict; use warnings; my ($M, $N) = (9999997, 3); my $step=0; my @ranges = map { my ($c,$d)=($step,$_*int($M/$N)); $step=$d; [ $c, $ +d-1 ] } 1 .. $N; $ranges[-1][1]=$M; print join(", ", map { "(".join("-",@{$_}).")" } @ranges), "\n"; $ perl 892828_b.pl (0-3333331), (3333332-6666663), (6666664-9999997)

Perhaps one of these will be close enough to inspire a better solution for you?

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Update: Added code tags to a bit of text so it renders correctly.


In reply to Re: Split range 0 to M into N non-overlapping (roughly equal) ranges. by roboticus
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.