Created
August 3, 2018 19:10
-
-
Save jasonknight/f7ec8072e75e65b94a8d1ade61fefff2 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
function splitNumber(n,l) { | |
if ( l == 1 ) { | |
return [n]; | |
} | |
var guess = Math.random(n / l + 1) * (((n/l) / 10 ) * 10); | |
return [guess].concat(splitNumber(n - guess,l - 1)) | |
} | |
// Test Code | |
for ( var i = 0; i < 10; ++i ) { | |
var test_case = Math.random(); | |
if ( test_case > 0.25 ) { | |
test_case = Math.floor(test_case * 100); | |
parts = 4; | |
} else { | |
test_case = Math.floor(test_case * 10); | |
parts = 7; | |
} | |
console.log("case:", test_case, splitNumber(test_case,parts).reduce((v,a) => a += v) == test_case, splitNumber(test_case,parts).length == parts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment