-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path010.js
51 lines (38 loc) · 1.45 KB
/
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
46
47
48
49
50
51
/*
Source: https://projecteuler.net/problem=10
Problem:
"The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million."
My Algorithm:
1. Use the Sieve of Eratosthenes method to generate prime numbers
The algorithm in a gif: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
2. Execute a reducer function to add up all the numbers in the array of prime numbers
*/
function sieveOfEratosthenes(n) {
let numbersUntilN = []
let upperLimit = Math.sqrt(n);
let primesUntilN = [];
for (let i = 0; i < n; i++) {
numbersUntilN.push(true); //makes an array from 2 to n-1
}
for (let i = 2; i <= upperLimit; i++) {
if (numbersUntilN[i]) {
for (let j = i * i; j < n; j += i) {
numbersUntilN[j] = false; //"cross out" the numbers that are multiples of i, like in the algorithm gif https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
}
}
}
//after the "crossing-out" is done, push the ones that are left to the primesUntilN array
for (let i = 2; i < n; i++) {
if(numbersUntilN[i]) {
primesUntilN.push(i);
}
}
return primesUntilN;
}
function solution(n) {
let primesUntilN = sieveOfEratosthenes(n);
const add = (a, b) => a + b;
return primesUntilN.reduce(add);
}
console.log(solution(2000000)); //outputs the answer!