use strict; use warnings; #======================================== # Algorithms #======================================== sub ranges_javafan { my (\$M, \$N) = @_; return [ map {[int(\$_*(\$M+1)/\$N) , int((\$_+1)*((\$M+1)/\$N))-1] } 0..\$N - 1]; } #---------------------------------------- sub ranges_javafan_buk { my (\$M, \$N) = @_; my \$STEP = ( \$M + 1 ) / \$N; return [ map [int( \$_ * \$STEP ), int( ( \$_+1 ) * \$STEP ) -1] , 0 .. \$N - 1]; } #---------------------------------------- sub ranges_elisheva_ikegami { my (\$M, \$N) = @_; my \$r = (\$M+1)%\$N; my \$q = (\$M+1-\$r)/\$N; my \$iEnd=0; return [ map { my \$iStart=\$iEnd; [\$iStart, (\$iEnd+=\$_) - 1] } ((\$q+1) x \$r, (\$q) x (\$N-\$r)) ]; } #======================================== # Check of final element of range #======================================== sub roundCheck { my (\$cr, \$M) = @_; my \$iTotal=0; my \$iErrors=0; for my \$m (1..\$M) { for my \$n (1..\$m) { \$iTotal++; my \$aRanges = \$cr->(\$m, \$n); my \$iFinal = \$aRanges->[-1][1]; if (\$iFinal != \$m) { \$iErrors++; # Uncomment here to see individual errors # --------------------------------------- # print "(\$m,\$n) ==> final=\$iFinal\n"; } } } printf "Error rate: %s\n", (\$iErrors*100/\$iTotal); } #------------------------------------------------------- print "Javafan:\n"; roundCheck(\&ranges_javafan, 100); print "Javafan_buk:\n"; roundCheck(\&ranges_javafan_buk, 100); print "elisheva_ikegami\n"; roundCheck(\&ranges_elisheva_ikegami, 100); #outputs (Debian Linux - Lenny, perl 5.10.0, 32-bit) Javafan: Error rate: 2.47524752475248 Javafan_buk: Error rate: 2.47524752475248 elisheva_ikegami Error rate: 0