-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path010.js
More file actions
26 lines (22 loc) · 673 Bytes
/
010.js
File metadata and controls
26 lines (22 loc) · 673 Bytes
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
/*
Problem 10: Summation of primes
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below n.
*/
const sieveBelow = limit => {
const primes = new Array(limit).fill(true);
if (limit >= 2) {
const sqrtlmt = Math.sqrt(limit);
for (let i = 2; i <= sqrtlmt; i++) {
if (primes[i])
for (let j = i * i; j < primes.length; j += i) primes[j] = false;
}
}
let answer = primes.map((el, i) => el && i !== 1 ? i : false).filter(el => el !== false);
answer.shift()
return answer;
}
const primeSummation = n => {
return sieveBelow(n).reduce((acc, curr) => acc + curr, 0)
}
console.log(primeSummation(2000000))