Javascript: Sum All Primes

Unrelated to passing the challenge - this solution has some serious efficiency concerns that will cause it to take a very long times as num gets big.

A few tips:

  1. Big arrays are slow. Only make arrays of information that you must have and cannot create otherwise.
  2. You don’t need to check if every number less that itself divides the number to know if the number is prime. (The largest possible factor of a number is it’s own square root)

Also, once you get a version that you think works quickly, it would be worth looking at the Sieve of Eratosthenes based approaches.

I did a bit of a write-up on performance of that approach here: JavaScript Performance Measurement & Variability

1 Like