Sum All Primes topic

Sum All Primes topic
0.0 0

#1

Tell us what’s happening:
this code works well for small value inputs but for the large values it is not working

Your code so far

function checkprime(i,numo)
{
  var count=0;
  for(var j=1;j<numo;j++)
    {
      if(i%j==0)
        count++;
    }
  if(count==2)
    return 1;
  return 0;
  
}
function sumPrimes(num) {
  var sum=0;
  for(var i=1;i<=num;i++)
    {
      
      if(checkprime(i,num))
        sum+=i;
    }
  
  return sum;
}

sumPrimes(977);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:


#2

I can see what’s wrong. 977 is a prime, which is why your code doesn’t work. I suggest you try it on smaller primes such as 3, 5 and 7 and try to think about why it’s not working for those. If you need more hints feel free to ask again.