Why won't this work with bigger inputs?

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.

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

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

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.

2 Likes

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

Thanks for the advice JeremyLT. Definitely still learning the ropes.
1 Like