in reply to Re^3: Split range 0 to M into N non-overlapping (roughly equal) ranges.
in thread Split range 0 to M into N non-overlapping (roughly equal) ranges.
OK, I just couldn't give up on it. I've come up with two final solutions:
$ cat 892828_e.pl #!/usr/bin/perl use strict; use warnings; my ($MIN, $MAX, $N) = (12345, 9999997, 6); ### Inline version: my @R = ( map {$_*int($MAX/$N)} 0 .. $N-1 ); $R[$_] = [$R[$_],$R[$_+1]//$MAX] for 0 .. $N-1; print join("; ", map(join("-",@$_),@R)), "\n\n"; ### Subroutine version: @R = make_ranges($MIN, $MAX, $N); print join("; ", map(join("-",@$_),@R)), "\n\n"; sub make_ranges { my ($min, $max, $N) = @_; return [$min, $max] if $N ==1; my $newmax = $min + int( ($max-$min)/$N ); return [$min, $newmax], make_ranges($newmax+1, $max, $N-1); } Roboticus@Roboticus-PC ~ $ perl 892828_e.pl 0-1666666; 1666666-3333332; 3333332-4999998; 4999998-6666664; 6666664- +8333330; 8333330-9999997 12345-1676953; 1676954-3341562; 3341563-5006171; 5006172-6670780; 6670 +781-8335389; 8335390-9999997
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
---|