Hidden Cubic numbers
This commit is contained in:
13
hidden-cubic-numbers/find_cubic_numbers.js
Normal file
13
hidden-cubic-numbers/find_cubic_numbers.js
Normal 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' ]
|
||||||
10
hidden-cubic-numbers/index.js
Normal file
10
hidden-cubic-numbers/index.js
Normal 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`;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user