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!