Created
March 18, 2015 16:00
-
-
Save aderowbotham/6af12ffb15fdec39375b to your computer and use it in GitHub Desktop.
Distribute Integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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