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.
Your code so far
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)
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0
.
Challenge: Problem 10: Summation of primes
Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/project-euler/problem-10-summation-of-primes