I have tried a few attempts on this problem working to make it more efficient each time. When I run it locally with node js it gives me the correct answer in less than a second, but when I try to run it on freeCodeCamp it tells me I do not get the right answer for primeSummation(2000000). I have run into this issue before when my code isn’t “efficient enough” for CodeCamp, but in this case I’m really struggling how to make it more efficient in order to pass all the tests on the freeCodeCamp website itself. Suggestions?!
Your code so far
function sieve(n){
let prime = [];
for(let i = 1; i < n + 1; i++){
prime.push(true);
}
prime[0]=false; // 0 not prime
prime[1]=false; // 1 not prime
let p = 2;
while(p*p<=n){
if(prime[p] == true){
for(let i = 2*p; i<=n; i += p){
prime[i] = false;
}
}
p += 1;
}
return prime;
}
function primeSummation(n){
let prime = sieve(n);
let sum = 0;
for (let i=0; i<prime.length; i++){
if(prime[i]==true){
sum = sum + i;
}
}
return sum;
}
console.log(primeSummation(2000000))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
.
Link to the challenge: