Created
April 23, 2022 13:38
-
-
Save TheeBryanWhite/0c340477ed107ad2a650e3ed366f6722 to your computer and use it in GitHub Desktop.
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
const minNum = 1; | |
const maxNum = 26; | |
let naeqSum = parseInt(process.argv[2]); | |
let allCombos = []; | |
let limit = 0; | |
const randomInt = (min, max) => { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
const isLessThanNaeq = (input) => { | |
if (naeqSum >= input) { | |
return true | |
} | |
} | |
const sortArray = (arr) => { | |
const sortedArr = arr.sort((a, b) => { | |
return a - b; | |
}); | |
return sortedArr; | |
} | |
const subtractFromNaeq = (sum) => { | |
let thisCombo = []; | |
// as long as naeqSum is greater than zero do this | |
while (sum > 0) { | |
// generate a random int within our range | |
let subtractThis = randomInt(minNum, maxNum); | |
// make sure that the rando is less than the remaining sum of naeqSum | |
if (isLessThanNaeq(subtractThis)) { | |
// push the rando to the current combo array | |
thisCombo.push(subtractThis); | |
// subtract the rando from naeqSum | |
sum -= subtractThis; | |
} | |
} | |
// sort the ints lowest to highest | |
return sortArray(thisCombo); | |
} | |
const init = () => { | |
// timeout after 1000 failures to push to allCombos | |
// there's a better way to do this but I'm not the person to figure it out | |
while (limit <= 1000) { | |
const pushThisCombo = subtractFromNaeq(naeqSum); | |
// only push thisCombo to our allCombos if it's not already there | |
// if it IS in the array, add 1 to our limit | |
if (!allCombos.includes(pushThisCombo)) { | |
allCombos.push(pushThisCombo); | |
} else { | |
limit += 1; | |
} | |
} | |
return false; | |
} | |
init(); | |
console.log(allCombos); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment