FYI, there are occasional one-off rounding error issues with this algorithm (possibly platform dependent), 2.5% of the time partitioning ranges of 1..100 or less. Depending on your goals that may or may not matter, but here are some examples :
$M,$N final array element (should be $M)
(14,11) ==> final=13
(14,13) ==> final=13
(29,11) ==> final=28
(29,13) ==> final=28
(29,22) ==> final=28
(29,26) ==> final=28
(59,11) ==> final=58
(59,13) ==> final=58
(59,22) ==> final=58
(59,26) ==> final=58
(59,44) ==> final=58
(59,52) ==> final=58
(59,55) ==> final=58
(100,49) ==> final=99
(100,98) ==> final=99
(100,99) ==> final=99
Test script
Update added test script and added parenthetical remark about platform dependency. |