Help with Sum All Primes

Help with Sum All Primes
0.0 0

#1

i struggle to find out if any division that is the same as v number stored in primes variable and if not… add it to variable… thank you


function sumPrimes(num) {
  var primes = [];
  var result = [];
  var i = 1;
  for (var j = 0; j < num; j++) {
    for (i; i < num; i++) {
      if (j % i === 0 && i >= 2 && primes.indexOf(Math.sqrt(i)) !== 0) {
        primes.push(i);
      }
      i++;
    }
  }
  console.log(primes);
  primes.push(2);
  var sum = primes.reduce(function (a, b) {
    if(b/a !== 0){
      return a + b;
    }
}, 0);

  return sum;
}
sumPrimes(10);

#2

I don’t understand why you need to “find out if any division that is the same as v number stored in primes variable”. Just grammatically I’m not sure what you’re saying but also when I look at your code I’m not sure what you are trying to do. You have the lines:

    if(b/a !== 0){
      return a + b;
    }

in your reduce function. This seems unnecessary. The purpose of the reduce is just to add the primes together. If you are trying to check if they are not same, then you want if (b/a !== 1) or just if (b===a). If you check (b/a!==0) that is mathematically checking if (b===0) so I’m not sure what you’re after. And remember that a is your accumulator, not an element in the array. I think your reduce should just be

  var sum = primes.reduce(function (a, b) {
      return a + b;
  }, 0);

That will do what it is supposed to do - sum your primes. (And to be honest, having tested your code, I think that is what your code was doing, just with an added if statement that did nothing.)

The real issue is your attempt to get primes. When I run your code, it gives me numbers that aren’t prime - it seems to just give me an array of odd numbers. If you fix that, your reduce will work fine.

I’m not sure I follow the algorithm you’re trying to use here. Let me explain in pseudo code how I approached it.

First, we need to check every number from 2 to num (inclusive) to see if they’re prime. If they are, push them onto our array. (Or we could have just added them to a summing variable, but let’s do it your way.)

Then, within that loop, we need to see if the new candidate number is prime. We can do this by looping from 2 to that number - not num!. (Remember that 1 is not prime.) We only need to check up to that number. If there are not numbers that divide evenly (if (i % j === 0 )) , then we push that number onto primes.

so:

loop i from 2 to num
  loop j from 2 to i
    if j divides evenly into i then
      break out of j loop // this is not a prime number
    push i onto primes array // if we reach this, it's because nothing divided into i, it's prime

That will get the job done. It’s not the only way, but it is straightforward. The j loop can be replaced by anything that checks if something is prime.

Ignore the next part if you want. It’s just algorithmic perfectionism.

There are certain optimizations you could do. For example, we could just automatically push 2 onto our array since we know it is prime. (Though we should to test to make sure num is greater than 1.) If we start our i loop at 3, then we could increment by 2, since we know that above 2, only odd numbers will be prime. And instead of a j loop, we could also skip the even numbers (no prime is divisible by an even number) or I think we could just run through our primes array, since any non-prime number will be divisible by a prime. (A potential huge time saver for large numbers, a good excuse to use a primes array instead of summing them in situ.) And we would only need to check primes up to and including the square root of i since any number will not be divisible by a number greater than it’s square root.


#3

Thank you for your explanation.