Skip to content

Instantly share code, notes, and snippets.

@adyngom
Last active September 14, 2017 01:20
Show Gist options
  • Select an option

  • Save adyngom/dbc3d7361476b2826d620242ff4ac871 to your computer and use it in GitHub Desktop.

Select an option

Save adyngom/dbc3d7361476b2826d620242ff4ac871 to your computer and use it in GitHub Desktop.
function getDecentNumber(N) {
let number = -1,
inc = 5,
five_str = "5",
three_str = "3",
five_num = 5,
three_num = 3;
// numberCast function will turn any string up to 16 digits
// into a number due to Javascript limitation on anything longer
let numberCast = (n) => (n.length <= 16) ? Number(n) : n;
if( N <= 2) { return number };
if ( N % 5 === 0 ) {
return numberCast(three_str.repeat(N));
}
if ( N % 3 === 0 ) {
return numberCast(five_str.repeat(N));
}
while (inc < N) {
if ( (inc % five_num) === 0 && ((N - inc) % three_num) === 0) {
number = `${five_str.repeat(N - inc)}${three_str.repeat(inc)}`;
break;
}
inc += five_num;
}
return numberCast(number);
}
// OLD - KEEPING WITH REFACTORED FOR REFERENCE
// function findDecentNumber(N) {
// var big = + Array(N).fill(5).join('');
// var number = -1;
// var count = 0, fives = N, threes = 0;
// var inc = 2, diff = 0, base = 0;
// if( N % 3 === 0) return big;
// while(count < N) {
// base = Math.pow(10, count);
// diff = 2 * base;
// big -= diff;
// fives -= 1;
// threes += 1;
// if( (fives % 3 === 0) && (threes % 5 === 0) ) {
// // exit and return number
// number = big;
// break;
// }
// count++;
// }
// return number;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment