Instantly share code, notes, and snippets.

Embed
What would you like to do?
Distribute Integers
// distribute a large integer as evenly as possible between any number of integer positions
// inputs
var total = 6;
var parts = 5;
var fractionFloored = Math.floor(total/parts);
var mod = total % parts;
var output = [];
for(var i = 0; i < parts; i++){
output.push(fractionFloored + Math.min(1, Math.max(0, mod)));
mod--;
}
console.log("distribute " + total + " between " + parts + " integer parts");
console.log(output);
// test
var totalTest = 0;
for(var i = 0; i < output.length; i++){
totalTest += output[i];
}
console.log("totalTest = "+totalTest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment