Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
arguments-optional.md
binary-agents.md
convert-html-entities.md
diff-two-arrays.md
dna-pairing.md
drop-it.md
everything-be-true.md
make-a-person.md
map-the-debris.md
missing-letters.md
pig-latin.md
search-and-replace.md
seek-and-destroy.md
smallest-common-multiple.md
sorted-union.md
spinal-tap-case.md
steamroller.md
sum-all-numbers-in-a-range.md
sum-all-odd-fibonacci-numbers.md
sum-all-primes.md
wherefore-art-thou.md
intermediate-javascript-calorie-counter
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
dictionaries
schema
test
.babelrc
LICENSE.md
create-challenge-bundle.js
crowdin.yml
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
2020-09-29 22:09:04 +02:00

1.8 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
a3bfc1673c0526e06d3ac698 Sum All Primes 5 16085

Description

A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4.

Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.

Instructions

Tests

tests:
  - text: <code>sumPrimes(10)</code> should return a number.
    testString: assert.deepEqual(typeof sumPrimes(10), 'number');
  - text: <code>sumPrimes(10)</code> should return 17.
    testString: assert.deepEqual(sumPrimes(10), 17);
  - text: <code>sumPrimes(977)</code> should return 73156.
    testString: assert.deepEqual(sumPrimes(977), 73156);

Challenge Seed

function sumPrimes(num) {
  return num;
}

sumPrimes(10);

Solution

function eratosthenesArray(n) {
    var primes = [];
    if (n > 2) {
        var half = n>>1;
        var sieve = Array(half);
        for (var i = 1, limit = Math.sqrt(n)>>1; i <= limit; i++) {
            if (!sieve[i]) {
                for (var step = 2*i+1, j = (step*step)>>1; j < half; j+=step) {
                    sieve[j] = true;
                }
            }
        }
        primes.push(2);
        for (var p = 1; p < half; p++) {
            if (!sieve[p]) primes.push(2*p+1);
        }
    }
    return primes;
}

function sumPrimes(num) {
  return eratosthenesArray(num+1).reduce(function(a,b) {return a+b;}, 0);
}

sumPrimes(10);