Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Split range 0 to M into N non-overlapping (roughly equal) ranges.

by jdrago999 (Pilgrim)
on Mar 15, 2011 at 04:34 UTC ( [id://893233]=note: print w/replies, xml ) Need Help??


in reply to Split range 0 to M into N non-overlapping (roughly equal) ranges.

My favorite way to do this:
#!/usr/bin/perl -slw use strict; use warnings 'all'; use POSIX 'ceil'; our $N //= 3; our $M //= 1e6; my $each = ceil($M / $N); my $last = 0; my @ranges = ( ); for( 1..$N ) { last unless $last <= $M; my $part = [ $last => $_ == $N ? $M : $last + ( $_ == $N ? $each : $ +each - 1 ) ]; $part->[1] = $M if $_ == $N; $last += $each; push @ranges, $part; }# end for() printf "%2d : from %7d to %7d (%7d)\n", $_, @{ $ranges[ $_ ] }, $ranges[ $_ ][ 1 ] - $ranges[ $_ ][ 0 ] + 1 for 0 .. $#ranges;

Produces the following:

john@e6510:~/Desktop$ ./ranges -N=1 -M=9999997 0 : from 0 to 9999997 (9999998) john@e6510:~/Desktop$ ./ranges -N=2 -M=9999997 0 : from 0 to 4999998 (4999999) 1 : from 4999999 to 9999997 (4999999) john@e6510:~/Desktop$ ./ranges -N=3 -M=9999997 0 : from 0 to 3333332 (3333333) 1 : from 3333333 to 6666665 (3333333) 2 : from 6666666 to 9999997 (3333332) john@e6510:~/Desktop$ ./ranges -N=4 -M=9999997 0 : from 0 to 2499999 (2500000) 1 : from 2500000 to 4999999 (2500000) 2 : from 5000000 to 7499999 (2500000) 3 : from 7500000 to 9999997 (2499998) john@e6510:~/Desktop$ ./ranges -N=5 -M=9999997 0 : from 0 to 1999999 (2000000) 1 : from 2000000 to 3999999 (2000000) 2 : from 4000000 to 5999999 (2000000) 3 : from 6000000 to 7999999 (2000000) 4 : from 8000000 to 9999997 (1999998)

Update: Now we properly calculate the number of ranges to create.

Update 2: This has a bug that I'm too tired and busy to figure out right now.

Replies are listed 'Best First'.
Re^2: Split range 0 to M into N non-overlapping (roughly equal) ranges.
by repellent (Priest) on Mar 15, 2011 at 05:12 UTC
    Bug?
    $ ranges -N=7 -M=9 0 : from 0 to 1 ( 2) 1 : from 2 to 3 ( 2) 2 : from 4 to 5 ( 2) 3 : from 6 to 7 ( 2) 4 : from 8 to 9 ( 2) 5 : from 10 to 11 ( 2) 6 : from 12 to 9 ( -2)

      You found it - and it's fixed now.

      Thanks for trying that out :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://893233]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-29 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found