Sum All Primes Efficiency Comparison

Hey so I just finished solving the sum all primes algorithm in the javascript intermediate algorithms section and as always I go check the help section to see the best and most advanced solutions and my solution seemed a lot more simple than the ones offered so I starting wondering about which solution was the quickest. After a bunch of googling, I read about the big O notation but deduced that I didn’t have enough experience to work out which solution was quickest, I wanted something to time each function and see the result, I then decided to hit the “Run Code” link just below the solutions and it was there that I learned about console.time, amazing, I found what I was looking for but something was still strange… After testing each of the four solutions I had at my disposal I noticed that the quickest was the basic solution, my simple solution followed as the runner up, in 3rd place was the intermediate solution and slowest of all was the advanced one.

So now I turn to the community, which solution would you consider to be the best and why?

Here is our winner, the basic solution:

function sumPrimes(num) {
  var res = 0;
  // FUnction to get the primes up to max in an array
  function getPrimes(max) {
    var sieve = [];
    var i;
    var j;
    var primes = [];
    for (i = 2; i <= max; ++i) {
      if (!sieve[i]) {
        // i has not been marked -- it is prime
        primes.push(i);
        for (j = i << 1; j <= max; j += i) {
          sieve[j] = true;
        }
      }
    }
    return primes;
  }
  // Add the primes
  var primes = getPrimes(num);
  for (var p = 0; p < primes.length; p++) {
    res += primes[p];
  }
  return res;
}

The runner up, my solution:

function sumPrimes(num) {
  let result = 0
  for(let x = 2; x <= num; x++){
    result += x;
    for(let y = 2; y < x; y++){
      if(x%y===0){
        result -= x;
        break;
      }
    }
  }
  return result;
}

3rd place, intermediate:

function sumPrimes(num) {
  // function to check if the number presented is prime
  function isPrime(number){
      for (i = 2; i <= number; i++){
          if(number % i === 0 && number!= i){
          // return true if it is divisible by any number that is not itself.
             return false;
          }
       }
       // if it passes the for loops conditions it is a prime
      return true;
  }
  // 1 is not a prime, so return nothing, also stops the recursive calls.
  if (num === 1){
    return 0;
  }
  // Check if your number is not prime
  if(isPrime(num) === false){
  // for non primes check the next number down from your maximum number, do not add anything to your answer
    return sumPrimes(num - 1);
  }

  // Check if your number is prime
  if(isPrime(num) === true){
  // for primes add that number to the next number in the sequence through a recursive call to our sumPrimes function.
    return num + sumPrimes(num - 1);
  }
}

and lastly, our slow but advanced solution:

function sumPrimes(num) {
  // step 1	
  let arr = Array.from({length: num+1}, (v, k) => k).slice(2); 
  // step 2
  let onlyPrimes = arr.filter( (n) => { 
    let m = n-1;
    while (m > 1 && m >= Math.sqrt(n)) { 
      if ((n % m) === 0) 
        return false;
        m--;
    }
      return true;
  });
  // step 3
  return onlyPrimes.reduce((a,b) => a+b); 
}

I appreciate any and all input, thanks in advance!

I like your solution. It doesn’t store any previous value, maybe it’s not the fastest but it’s the most efficient in memory use, since it only need 4 primitive variables.
That said, the first one is doing more than summing all prime numbers it also stores them in an array, which makes it useful in cases where you need them individually. The 3rd uses recurrency, it just sums. The 4th uses many built-in prototype functions and objects, very inefficient without any reason. My favorites are the second for performing efficiently the sum and the first for been capable of storing individually the prime numbers.
It would be good for better searchability, if you wouldn’t mind changing the title in something like: “primes sum function efficiency comparison”

1 Like

Ah, great feedback, so essentially the basic solution is by far the best solution due to speed, simplicity and re-usability while the more advanced solutions are over complicating the problem unnecessarily, that makes sense, thanks so much for the feedback, and ill be sure to change title as suggested :slight_smile:

Oh of course, how shortsighted of me, I apologise for my ignorance, and thank you for the spoiler advice, in regards to posting the guide solutions I was simply trying to cut out the middle man so people reviewing wouldn’t have to go find what i was talking about but will definitely take what you have said into consideration for next time and simply post the link to the guide page. So, which solution do you think is the most efficient?