Skip to content

Instantly share code, notes, and snippets.

@aderowbotham
Created March 18, 2015 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aderowbotham/6af12ffb15fdec39375b to your computer and use it in GitHub Desktop.
Save aderowbotham/6af12ffb15fdec39375b to your computer and use it in GitHub Desktop.
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