-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path010.js
45 lines (35 loc) · 815 Bytes
/
010.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Summation of Primes
* Computes the sum of all primes below two million
*
* Ashen Gunaratne
*
*/
/**
* @function sum
* @summary Computes the sum of all primes below n
* @param {Number} n
* @description Returns the sum of all primes below the parameterised ceiling n
*/
const sum = function computeSumOfPrimes(n) {
let ceil = 1;
let accumulator = 0;
for (let index = 2, divisor = 3; index < n;) {
if (index % divisor === 0) {
ceil = Math.sqrt(index += 1);
divisor = 2;
continue;
}
if ((divisor += 1) > ceil) {
accumulator += index;
ceil = Math.sqrt(index += 1);
divisor = 2;
}
}
return accumulator;
};
// tests
console.assert(sum(10) === 17, `expected 17 got ${sum(10)}`);
// answer
console.log(sum(2000000));