-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler010.cpp
More file actions
46 lines (35 loc) · 865 Bytes
/
Copy pathprojectEuler010.cpp
File metadata and controls
46 lines (35 loc) · 865 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
/* 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 two million.
*/
#include <iomanip>
#include <iostream>
using namespace std;
bool isPrime (double x) {
bool prime = true;
double upperLimit = sqrt (x);
if (upperLimit / 2 == floor (upperLimit / 2)) {
upperLimit++;
}
for (int divisor = 2; divisor <= int(upperLimit); divisor++) {
if (x / (double)divisor == floor (x / (double)divisor)) {
prime = false;
break;
}
}
return prime;
}
void main () {
double primeCap = 2000000;
double primeSum = 2; // Start with the first prime already in the sum.
double num = 3;
while (num < primeCap) {
if (isPrime (num)) {
primeSum += num;
}
num += 2;
}
cout << setprecision (0) << fixed;
cout << primeSum << endl;
}