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?

Description

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)
JAVASCRIPT
OUTPUT
Results will appear here.