Hidden Cubic numbers

This commit is contained in:
2019-11-15 11:44:39 +01:00
parent 0028fed09b
commit 4ba8eafed8
2 changed files with 23 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
// cubic numbers
const res = [];
for (let i = 0; i < 1e3; ++i) {
const str = ('000' + i).slice(-3);
if (i === str.split``.reduce((a, b) => a + b**3, 0)) {
res.push(str);
}
}
console.log(res);
// [ '000', '001', '153', '370', '371', '407' ]

View File

@@ -0,0 +1,10 @@
// https://www.codewars.com/kata/hidden-cubic-numbers
function isSumOfCubes(s) {
const cns = ['0', '1', '153', '370', '371', '407'];
const n = s.match(/[0-9]+/g) || [];
const nr = n.reduce((a, b) => [...a, ...b.match(/[0-9]{1,3}/g)], []).map(n=>(+n)+'');
const f = nr.filter(n => cns.indexOf((+n)+'') !== -1);
if (!f.length) return 'Unlucky';
return `${f.join` `} ${f.reduce((a,b) => +b + a, 0)} Lucky`;
}