# Help with Sum All Primes

**URL:** <https://forum.freecodecamp.org/t/help-with-sum-all-primes/122340>\
**Category:** General\
**Created:** [June 1, 2017, 3:30pm UTC](https://forum.freecodecamp.org/t/help-with-sum-all-primes/122340 "2017-06-01T15:30:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VaclavF](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vaclavf/32/42849_2.png) [@VaclavF](https://forum.freecodecamp.org/u/VaclavF)\
**Post date:** [June 1, 2017, 3:30pm UTC](https://forum.freecodecamp.org/t/help-with-sum-all-primes/122340/1 "2017-06-01T15:30:34Z")

</div>

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

```auto

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);

```

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [June 1, 2017, 5:26pm UTC](https://forum.freecodecamp.org/t/help-with-sum-all-primes/122340/2 "2017-06-01T17:26:33Z")

</div>

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:

```auto
    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

```auto
  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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![VaclavF](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vaclavf/32/42849_2.png) [@VaclavF](https://forum.freecodecamp.org/u/VaclavF)\
**Post date:** [June 3, 2017, 4:20pm UTC](https://forum.freecodecamp.org/t/help-with-sum-all-primes/122340/3 "2017-06-03T16:20:29Z")

</div>

Thank you for your explanation.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:56pm UTC](https://forum.freecodecamp.org/t/help-with-sum-all-primes/122340/4 "2021-01-17T20:56:28Z")

</div>

Closed on January 17, 2021, 8:56pm UTC


