-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP10.js
56 lines (44 loc) · 1.03 KB
/
P10.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
52
53
54
55
56
/* ====== Problem 10 =================================== *
*
* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
*
* Find the sum of all the primes below two million.
*
* ========================================**/
const MAX_VALUE = 2e6;
function isPrime(x) {
if (x === 1 || x === 2) {
return true;
}
for (var i = 2; i < x; i++) {
if (x !== i && x % i === 0)
return false;
if ((i+1) === x)
return true;
}
}
function sumOfPrimes() {
var sum = 0,
base = 2;
do {
if (isPrime(base))
sum += base;
base++;
} while (base < MAX_VALUE);
return sum;
}
function solution() {
var answer = sumOfPrimes();
console.log(`The solution to problem 10 is: ${answer}`);
}
solution();
/* ====== Meta ========================================= *
*
* Estimated time of completion: 10
* Search engine used: No
* Concepts learned:
*
* Additional notes: This took ages to complete. Is there
* a way to calculate this quicker?
*
* ========================================**/