 # Intermediate Algorithm Scripting: Sum All Primes - argh

Intermediate Algorithm Scripting: Sum All Primes - argh
0

#1

After pulling my hair out trying to figure out why my code wasn’t passing (although it did on repl.it), I found some posts saying that FCC’s tests include a timer and my code apparently fails it. First, and I’m not complaining about a free service, but it’d sure be nice if the results pane said so, rather than just saying “sumPrimes(977) should return 73156” (which my code actually does). But more to the point, could someone please tell me what is timing out here? My code checks for the easy ones (2,3,5) early in the process, so I’m hoping that would be efficient enough:

``````function sumPrimes(num) {
var sum = 0;
for (let current = 2; current <= num; current ++)  {
var isprime = true;
for (let i = 2; i< current; i++) {
if (current % i == 0) {
isprime = false;
break;
}
}
if (isprime) {
sum = sum + current;
}
}
return(sum);
}
``````

Thanks again to all.

#2

by ‘timing out’ it means that your algorithm as a whole is taking too long.
try optimizing the algorithm itself (versus a specific line of code).
there are ideas on how to do that in the forum.

(for eg. when I tried this, I used common divisibility rules to optimize my prime finding algorithm. there are other ideas out there…)

#3

Another thing to perhaps take a look at is the sieve of eranthoses. It might give you another method of figuring out primes.