# Tests on Euler 10 time out

Tell us what’s happening:

The code is correct, but the tests fail because the last one times out. Is it a bug or intended behaviour? I know for a fact that the code is correct, because I ran it in my console and the result is correct (it took 30s to compute). But on the FCC website the first three tests are passed, but the last one times out and returns a wrong value. Should I try to optimize or just pass it and go to the next problem? I’m not sure if there are any quicker ways to calculate primes.

``````let primes = [2, 3, 5]

function isPrime (number) {
for (var i = 0; i < primes.length; i++) {
if (number % primes[i] === 0) {
return false
}
}
return true
}

function primeSummation(n) {
for (let num = 6; num < n; num += 6) {
if (isPrime(num - 1)) {
primes.push(num - 1)
}
if (isPrime(num + 1) && num + 1 < n) {
primes.push(num + 1)
}
}
return primes.reduce((a, b) => a + b)
}

primeSummation(2000000)
``````

User Agent is: `Mozilla/5.0 (X11; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0`.

Challenge: Problem 10: Summation of primes

maybe you need an algorithm more efficient
I will give you an hint:
to know if 23 is a prime or not, you just need to check if it is not divisible by numbers just up to 5
(it is sufficient to check up to the square root of the number, not above that)

1 Like

Thanks, I’ll try to wrap my head around that and let you know if it helped.

Yay, it reduced the computation time to a fraction of a second!
Still the tests failed, interestingly. It was a problem with the scope of the primes array. It was not reset between tests. I fixed it by moving the declaration into the main function and then passing it as the second parameter to the isPrime function.

1 Like