Sum All Primes (Medium)
Good evening! Here's our prompt for today.
You may see this problem at Godaddy, Apple, Dropbox, Workday, Coursera, Tesla, Ibm, Gitlab, Mailchimp, Blend, Grammarly, Zoom, Cvent, and New Relic.
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
n
will always be a non zero positive integer <=100000
- Expected time complexity :
O(nlogn)
- Expected space complexity :
O(n)
xxxxxxxxxx
34
var 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 {
OUTPUT
Results will appear here.