![]() |
|
Welcome to the Monastery | |
PerlMonks |
Re: Split range 0 to M into N non-overlapping (roughly equal) ranges.by ELISHEVA (Prior) |
on Mar 12, 2011 at 19:40 UTC ( #892856=note: print w/replies, xml ) | Need Help?? |
Why not adapt the solution ikegami suggested a few weeks back on the Near equal partitions. thread?
Sample output
It ensures that partitions never differ by more than 1 in size and avoids rounding errors. On benchmark results it is also marginally faster (4-8%) than the code in the OP (actual result varies between runs)
Benchmark code
Update: added that it avoids rounding errors. Update: minor revision to account for OP desiring range to go from 0..$M ($M+1 values) not 0..($M-1), i.e. $M values.
In Section
Seekers of Perl Wisdom
|
|