# Why won't this work with bigger inputs?

**URL:** <https://forum.freecodecamp.org/t/why-wont-this-work-with-bigger-inputs/408985>\
**Category:** General\
**Created:** [July 10, 2020, 11:36pm UTC](https://forum.freecodecamp.org/t/why-wont-this-work-with-bigger-inputs/408985 "2020-07-10T23:36:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![puglisi1224](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/puglisi1224/32/135798_2.png) [@puglisi1224](https://forum.freecodecamp.org/u/puglisi1224)\
**Post date:** [July 10, 2020, 11:36pm UTC](https://forum.freecodecamp.org/t/why-wont-this-work-with-bigger-inputs/408985/1 "2020-07-10T23:36:39Z")

</div>

This function takes a number argument and returns the sum of all prime numbers up to that argument. It only works up to a certain number and I’m wondering if its just that the calculations get too hefty for the computer or if something changes about the execution of the code at higher numbers. I’m interested in any input.

```auto
function sumPrimes(num) {
  // Assign an empty array for prime numbers
  let primeArray = [];
  // Generate numbers up to the user provided input
  for(let i = 2; i <= num; i++){
    /*Assign an empty array to store the number of times "i" can be divided without any remainder*/ 
    let numOfDivisors = [];
    /*Loop through each possible number that "i" can be divided by and push a 1 for each time that that it divides through with no remainder*/ 
    for(let j = 1; j <= i; j++){
      if(i%j === 0){
        numOfDivisors.push(1);
      }
    }
    // Push each number that has only 2 divisors to the prime number array
    if(numOfDivisors.length === 2){
      primeArray.push(i);
    }
  }
  // Add all of the values in the array together using .reduce()
  return primeArray.reduce((acc,curr) => acc + curr)
}

console.log(sumPrimes(1000));

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [July 10, 2020, 11:47pm UTC](https://forum.freecodecamp.org/t/why-wont-this-work-with-bigger-inputs/408985/2 "2020-07-10T23:47:48Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [July 10, 2020, 11:50pm UTC](https://forum.freecodecamp.org/t/why-wont-this-work-with-bigger-inputs/408985/3 "2020-07-10T23:50:35Z")

</div>

Welcome to the forum!

You’re running into a classic efficiency problem with this type of approach. You are making `num*num` comparisons with your `if (i%j ===0)`, which means that your code will rapidly get slower as you increase `num`.

Specifically, `num = 1000` will take `100` times longer than `num = 100`, and `num = 10000` will take `10000` times longer than `num = 100`.

Eventually, Free Code Camp’s infinite loop protection triggers and stops your function.

I think you can make some small changes to make this solution a little bit better and past the Free Code Camp test suite (Do you need to keep checking if you found a divisor? Do you need to check all even divisors? Do you need to check all divisors up to size j? Do you need an array of the primes or will a running total work?)

To get a really fast solution, I’d look at the Sieve of Eratosthenes.

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [July 10, 2020, 11:50pm UTC](https://forum.freecodecamp.org/t/why-wont-this-work-with-bigger-inputs/408985/4 "2020-07-10T23:50:41Z")

</div>

I would split this into two functions. Create a function that finds all the prime numbers between 2 and N (I’ll call it findPrimes) and have that function return an array or Set of the primes. Then the sumPrimes function calls findPrimes() and merely iterates through the array/Set and adds them all together.

As for how to find the primes, there is no harm in searching for algorithms to do this. I’m not talking about finding the code and copy/pasting. I mean find an explanation of how the algorithm works and then you implement it in code. There are some ways to speed up the process but unless you remember your high school math you might not know how to do it off the top of your head (I didn’t, I had to look it up too).

---

<div class="post-metadata">

**Author:** ![puglisi1224](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/puglisi1224/32/135798_2.png) [@puglisi1224](https://forum.freecodecamp.org/u/puglisi1224)\
**Post date:** [July 11, 2020, 12:13am UTC](https://forum.freecodecamp.org/t/why-wont-this-work-with-bigger-inputs/408985/6 "2020-07-11T00:13:03Z")

</div>

```auto
Thanks for the advice JeremyLT. Definitely still learning the ropes.

```

---

<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:** [June 1, 2021, 6:47pm UTC](https://forum.freecodecamp.org/t/why-wont-this-work-with-bigger-inputs/408985/7 "2021-06-01T18:47:25Z")

</div>

Closed on June 1, 2021, 6:47pm UTC


