Good morning! Here's our prompt for today.
You're given a number n. Can you write a method sumOfAllPrimes that finds all prime numbers smaller than or equal to n, and returns a sum of them?

For example, we're given the number 15. All prime numbers smaller than 15 are:
2, 3, 5, 7, 11, 13
They sum up to 41, so sumOfAllPrimes(15) would return 41.
Constraints
nwill always be a non zero positive integer <=100000- Expected time complexity :
O(nlogn) - Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxxvar assert = require('assert');​function sumOfPrimes(n) { // fill this in return n;}​console.log(sumOfPrimes(7));console.log(sumOfPrimes(15));​try { assert.deepEqual(2, sumOfPrimes(2), 'sumOfPrimes(2) should equal 2');​ console.log('PASSED: ' + '`sumOfPrimes(2)` should equal `2`');} catch (err) { console.log(err);}​try { assert.deepEqual(129, sumOfPrimes(30), 'sumOfPrimes(30) should equal 129');​ console.log('PASSED: ' + '`sumOfPrimes(30)` should equal `129`');} catch (err) { console.log(err);}​try { assert.deepEqual(381, sumOfPrimes(55), 'sumOfPrimes(55) should equal 381');​We'll now take you through what you need to know.
How do I use this guide?
Step 1: Gathering Prime Number Candidates
We'll start by creating an array of integers up to n+1. Since prime numbers must be natural numbers greater than 1, we can discard 0 and 1.
Here's a visual representation of what we're doing:

Here's how we initialize our candidates in both JavaScript and Python:
1const n = 4;
2const numsLessThanN = Array.from(
3 { length: n + 1 }, function(val, idx) {
4 return idx;
5 }
6).slice(2);This provides us with all potential prime number candidates smaller than n.
Step 2: Filtering Prime Numbers
With our candidates ready, we must now filter them to identify the prime numbers.
The Naive Approach
We could manually check each number by looping through every number from 2 up to the current one, verifying if the current number is divisible by any other number. If it's divisible, it's not a prime number!
Here's a simple implementation:
1function isPrime(num) {
2 for (let i = 2; i < num; i++) {
3 if (num % i === 0) return false;
4 }
5 return num > 1;
6}
7
8console.log(isPrime(7));Efficiency Considerations
While this naive approach works, it's pretty slow. The manual check is an O(n) operation, and since we need to run it for each number less than the input n, the overall function has a time complexity of O(n^2).
Can we improve this? Stay tuned, and we'll explore more efficient ways to identify prime numbers in our next lessons!
xxxxxxxxxxfunction isPrime(num) { for (let i = 2; i < num; i++) { if (num % i === 0) return false; } return num > 1;}​console.log(isPrime(7));The easiest way is through this piece of logic: while a number can be prime and greater than 1, try dividing it by prime numbers greater than its square root. For example, say we have 6-- it's square root is ~2.449. So we try to divide it by 3, the next greater prime number, but we realize it is divisible and thus not prime. We'll expand on the logic for this shortly!

1const primes = numsLessThanN.filter((num) => {
2 let primeCandidate = num - 1;
3
4 while (primeCandidate > 1 && primeCandidate >= Math.sqrt(num)) {
5 if (num % primeCandidate === 0) return false;
6 primeCandidate--;
7 }
8
9 return true;
10});Complexity of Final Solution
Let n be the input number. We fill an array of size n to keep track of potential prime candidates for O(n) space complexity, and our while loop that updates our array performs roughly log n work over n iterations for O(n log n) time complexity.
Here's why it works: if a number is divisible by a prime number, then it itself is not a prime number.
On the flip side, the number must be divisible beyond its square root. The intuition here saves us several calculations, and can be understood with the following:
- If
nisn't prime, it should be able to be factored intoxandy. In our previous example,6could be factored into2and3. - If both
xandywere greater than the square root ofn, it would confirm thatx * ywould produce a number larger thann. In this case, only3is larger than the square root. - This means at least one of the factors needs to be less than or equal to the square root of
n. If we don't find any factors that fit, that are greater than or equal to the square root, we know thatnmust be a prime number. However, we found3-- so6can't be prime.

Let's take the number 7, and run the numbers smaller than it through this filter:
- Starting at
7, passes the conditions, returnstrue. 6, we find that6is divisible by3, so remove it.5passes the conditions, returnstrue.4is divisible by2, get rid of it as well.3passes the conditions, returnstrue.2passes the conditions, returnstrue.- We throw out
0and1since they're not prime.
So we get 17 once they're summed up.

One Pager Cheat Sheet
- Write a method
sumOfAllPrimesthat takes a numbernand returns the sum of all prime numbers smaller than or equal ton. - We can use an array of length
n+1initialized with natural numbers starting from 2, and removing 0 and 1, to programmatically determine what numbers are prime. - We can figure out the prime number candidates that are smaller than
nwith anO(n^2)operation by looping through every number from2to the current one and seeing if it is divisible. - We can check for prime numbers efficiently by looping over the numbers below
nand performing alog ntime operation to check divisibility by prime numbers larger than the square root ofn. - We can use a filter that requires a number to be divisible beyond its square root in order to determine if it is a prime number, which allows us to efficiently find and sum all prime numbers.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx}var assert = require('assert');​function sumOfPrimes(n) { const numsLessThanN = Array.from({ length: n + 1 }, function (val, idx) { return idx; }).slice(2);​ const primes = numsLessThanN.filter((num) => { let primeCandidate = num - 1;​ while (primeCandidate > 1 && primeCandidate >= Math.sqrt(num)) { if (num % primeCandidate === 0) return false; primeCandidate--; }​ return true; });​ return primes.reduce((prime1, prime2) => prime1 + prime2);}​try { assert.deepEqual(2, sumOfPrimes(2), 'sumOfPrimes(2) should equal 2');​ console.log('PASSED: ' + '`sumOfPrimes(2)` should equal `2`');} catch (err) { console.log(err);}​try { assert.deepEqual(129, sumOfPrimes(30), 'sumOfPrimes(30) should equal 129');​You're doing a wonderful job. Keep going!
If you had any problems with this tutorial, check out the main forum thread here.

