-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler10.cpp
More file actions
50 lines (40 loc) · 914 Bytes
/
Copy patheuler10.cpp
File metadata and controls
50 lines (40 loc) · 914 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// Created by Henry Glover on 8/23/23.
//
/* the sum of the primes below 10 is 2 + 3 + 5 + 7 = 17
*
* find the sum of all the primes below two million
*
*/
//
// Created by Henry Glover on 8/27/23.
//
#include <iostream>
bool isPrime(int n) {
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
// why does this code work so much better than my code : a: a math thing i dont know
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
int main() {
long long totalPrime = 0;
for (int i = 2; i < 2000000; i++) {
if (isPrime(i)) {
totalPrime += i;
}
}
std::cout << "Total sum of prime numbers under 2000000 = " << totalPrime << std::endl;
return 0;
}