Tell us what’s happening:
I’m working on some of the Euler problems and I got kind of stuck with number 3. The code I wrote so far (though pretty clunky) works for the other numbers on the check list but not for 600851475143. I know there are other ways to finish the problem but I’m wondering what mistake I’m making or if there is a limitation with arrays and the size of the numbers they can hold. Any help would be appreciated.
Your code so far
function largestPrimeFactor(number) {
var prime = [];
var factors = [];
for (var i = 0; i <= number; i++) {
for (var j = 0; j < i; j++) {
if (i % j === 0 && number % i === 0) {
prime.push(i);
}
}
}
for (var n = 0; n <= prime.length; n++) {
for (var k = 2; k < prime[n]; k++) {
if (prime[n] % k === 0) {
prime.splice(n)
}
}
}
var largest = Math.max(...prime);
return prime;
}
console.log(largestPrimeFactor(600851475143));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
.
Link to the challenge: