sumPrimes

Medium · Number Theory · 2 test cases

Sum all the prime numbers up to and including the provided number.

Examples

sumPrimes(10) → 17
sumPrimes(977) → 73156

Starter code

function sumPrimes(num) {

}

Complexity of the optimal solution

Time O(n * sqrt(n)), space O(sqrt(n)). This is a trial division method. For each number up to n, it checks for divisibility against already found primes. This is inefficient. A better approach like the Sieve of Eratosthenes would be O(n log log n). Space is for storing the primes.

Solve sumPrimes in the browser