Is it efficient? (sum primes)

This solution is very basic and simple, however I don’t think its the most efficient one, is it the most efficient one? if not then please write down the most efficient one. By the way my code looks pretty messy so any tips for making it look cleaner will be appriciated (rewriting it so it will look better is good)

My code so far


function sumPrimes(num) {
var nums = [];
//an array that contains all numbers from 2-num (1 isnt 
//prime either divisble)
for (let i = 2; i <= num; i++) {nums.push(i)}
//filters the array by checkung if any number there is divisble by another number that's not the same
return nums.filter(function(val) {
for (let j = 2; j <= num; j++) {
if (val % j === 0 && j !== val) {return false}
}
return true
})
//sums all the numbers of the filtered array
.reduce((sum, a) => sum + a)
}

sumPrimes(10);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Sum All Primes

Link to the challenge:

Hey there!

Let’s assume there would be some kind of “machine”, that can search the internet.
Furthermore let’s assume you would enter something like “fastest algorithm sum primes”.

How could you use this to answer your own question?


As a beginner, in ~99.99% of the time your solution is not the fastest or the most memory-saving algorithm. Does it even matter?

You can get the fastest algorithm by punching ‘prime number sieve’ into Google. I have a post somewhere on the forum where I implemented it (but I’m on my phone so it’s hard for me to dig through my post history right now).