Optimization (CodeWars Integers: Recreation One)

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Optimization (CodeWars Integers: Recreation One)



I managed to find two algos for this CodeWars challenge (https://www.codewars.com/kata/integers-recreation-one/train/javascript). Unfortunately, they are not fast enough (> 12000ms).



Any suggestions on how to improve my code ?



v1 :


const listSquared = (m, n) => {
const result = ;
for (let i = m; i <= n; i++) {
const divisorsOfi = ;
for (let j = 0; j <= i; j++) {
if (i % j === 0) {
divisorsOfi.push(Math.pow(j, 2))
}
}
let sumOfDivisorsOfi = 1;
if (divisorsOfi.length > 1) {
sumOfDivisorsOfi = divisorsOfi.reduce((a, b) => a + b);
}
if (Number.isInteger(Math.sqrt(sumOfDivisorsOfi))) {
result.push([i, sumOfDivisorsOfi]);
}
}
return result;
}



v2:


const listSquared = (m, n) => {
const result = ;
for (let i = m; i <= n; i++) {
let sumOfSqrtDivisorsOfi = divisors(i);
if (Number.isInteger(Math.sqrt(sumOfSqrtDivisorsOfi))) {
result.push([i, sumOfSqrtDivisorsOfi]);
}
}
return result;
}

const divisors = (n) => [...Array(n + 1).keys()].slice(1)
.reduce((s, a) => s + (!(n % (a)) && Math.pow(a, 2)), 0);



Thanks in advance !









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

x i2nV9z76Q GrT J j99W eeEYTz,FD,Yz6NukKiXhkU3pIk 8
RxENWSO0mqSK1,RYiX z6RzBSESl2fHNxVLdWr1a,8cbRFh

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham