Optimization (CodeWars Integers: Recreation One)

Multi tool use


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.