 # Sum All Primes, working on my PC but failing tests

Tell us what’s happening:

Hey all! I’m running this exact same code on my laptop, locally, and when I give it 977 I’m getting the correct output (73156), is this a memory issue? Hope there’s a fix! Thanks everyone!

``````
function isPrime(num) {
let count = 0;
for (let i = 1; i < num+1; i++) {
if (num % i === 0) {
count++;
}
if (count > 2) {
return false;
}
}
return true;
}

function sumPrimes(num) {
let sum = 0;
for (let i = 2; i <= num; i++) {
if (isPrime(i)) {
sum += i;
console.log(i);
}
}
return sum;
}

console.log(sumPrimes(977));
``````

User Agent is: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/75.0.3770.90 Chrome/75.0.3770.90 Safari/537.36`.

you may be triggering the infinite loop protection on the editor here, if your code takes too much to run it is stopped. you will need a more efficient code

I see, I imagined it was something of the like
Thanks, I’ll try to make it better

Alright, I’m kinda lost.
If you guys could point me in the right direction i’d be really grateful, I got told I should look into recursion but I’m not sure.

both your loops check using `i++` check till the number itself- it is a bit long like that. you could do a few searches online to see what you find out as algorithms

or, do you know that to check if `19` is prime you don’t need to check if it is divisible by `18`, `15` or `10` - the highest number you need to check is 5 (which is the square root rounded up)

also I suggest the `isPrime` function loop starts at `let i = 2` and return false as soon as it find something - there is no need for `count` variable, and you save an iteration for all numbers to check

1 Like