This commit adds the pre-existing challenge guide topics in the forum to the forntmatter of their challenge markdown files.
1.9 KiB
1.9 KiB
id, title, isRequired, challengeType, forumTopicId
id | title | isRequired | challengeType | forumTopicId |
---|---|---|---|---|
a3bfc1673c0526e06d3ac698 | Sum All Primes | true | 5 | 16085 |
Description
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);