Tell us what’s happening:
So I tried to follow the pseudocode on the sieve wiki page and this is what I have, and this passes when the test num is 10. The issue is 977, it says the sum is 72,179. I don’t really know whats going on. I also tried letting the last for loop be i <A.length instead of num and there was no difference. Advice is always appreciated.
Your code so far
function sumPrimes(num) {
let currentNumber = 0;
let sum = 0;
let A = [];
//An array of booleans from [2-num] set to true
for (let bool = 2; bool <= num; bool++){
if (bool <= num){
A.push(true);
}
}
//supposed to set prime vals true and non primes false
for (let i = 2; i <= Math.sqrt(num); i++){
if(A[i] === true){
for(let j = Math.pow(i,2); j <= num; j+=i){
A[j] = false;
}
}
}
//goes through and adds the primes
for(let i = 2; i <= num; i++){
if(A[i] === true){
currentNumber = i;
sum += currentNumber;
}
}
return sum;
}
sumPrimes(977);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0
Challenge: Intermediate Algorithm Scripting - Sum All Primes
Link to the challenge: