2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: a3bfc1673c0526e06d3ac698
|
|
|
|
title: Sum All Primes
|
|
|
|
challengeType: 5
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 16085
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
2019-11-30 16:33:37 -06:00
|
|
|
|
|
|
|
A <dfn>prime number</dfn> 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.
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
tests:
|
|
|
|
- text: <code>sumPrimes(10)</code> should return a number.
|
2019-07-27 21:16:04 -07:00
|
|
|
testString: assert.deepEqual(typeof sumPrimes(10), 'number');
|
2018-10-04 14:37:37 +01:00
|
|
|
- text: <code>sumPrimes(10)</code> should return 17.
|
2019-07-24 01:56:38 -07:00
|
|
|
testString: assert.deepEqual(sumPrimes(10), 17);
|
2018-10-04 14:37:37 +01:00
|
|
|
- text: <code>sumPrimes(977)</code> should return 73156.
|
2019-07-24 01:56:38 -07:00
|
|
|
testString: assert.deepEqual(sumPrimes(977), 73156);
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function sumPrimes(num) {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
sumPrimes(10);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
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);
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|