http://www.perlmonks.org?node_id=882284


in reply to Near equal partitions.

Yup. Your brain is definitely on fry :-) Think %.

sub part { my ($n, $m) = @_; my $r = $n%$m; # <== my @parts = (($n-$r)/$m) x $m; # <== my $i=0; $parts[$i++]++ while $r--; return @parts; }

Update: code sample to reflect outcome of Q&A with ikegami.

Using ikegami's elegant list generation, but without risk of floating point rounding errors:

sub part { my ($n, $m) = @_; my $r = $n%$m; my $q = ($n-$r)/$m; # avoid int($n/$m) to prevent fp rounding errors return ($q+1) x $r, ($q) x ($m-$r); }