Created
March 8, 2021 01:38
-
-
Save tevko/601da6e7190ce77f8b529380972c1579 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
// https://www.codewars.com/kata/556206664efbe6376700005c/solutions/javascript/me/best_practice | |
const isPolydivisible = (n, b) => n.toString().split('').every( | |
(d, i, a) => (a.join('').slice(0, i + 1)) | |
.split('').reverse() | |
.map(num => Number(num) || CHARS.indexOf(num)) | |
.reduce((a, c, i) => | |
(c * (Math.pow(b, i))) + Number(a) | |
) | |
% (i + 1) === 0 | |
); | |
const getPolydivisible = (n, b) => { | |
const convertToBaseFromTen = (num, base, concat = '') => { | |
if (Math.floor(num / base) >= 1) { | |
return convertToBaseFromTen(Math.floor(num / base), base, concat + (num % base > 9 ? CHARS[num % base] : num % base)) | |
} else { | |
return (concat + Math.floor(num % base)).split('').reverse().join('') | |
} | |
}; | |
const deducer = n >= b ? b : 0; | |
let inc = deducer; | |
let dec = n; | |
let highestPoly; | |
while (dec > deducer) { | |
const numInBase = convertToBaseFromTen(inc, b); | |
if (isPolydivisible(numInBase, b)) { | |
highestPoly = numInBase; | |
dec--; | |
} | |
inc++; | |
} | |
return n <= b ? CHARS[n - 1] : highestPoly; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment