-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler007.cpp
More file actions
49 lines (38 loc) · 865 Bytes
/
Copy pathprojectEuler007.cpp
File metadata and controls
49 lines (38 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
47
48
49
/* Problem 7
10001st Prime
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
we can see that the 6th prime is 13.
What is the 10001st prime number?
*/
#include <iostream>
#include <math.h>
using namespace std;
bool isPrime (float x) {
bool prime = true;
float sqroot = sqrt (x);
int upperLimit = floor (sqroot);
if (upperLimit % 2 == 0) {
upperLimit++;
}
for (int divisor = 2; divisor <= upperLimit; divisor++) {
if (x / (float)divisor == floor (x / (float)divisor)) {
prime = false;
break;
}
}
return prime;
}
void main () {
int nthPrime = 10001;
int primeCount = 2; // start from 3
float num = 3;
float nextPrime = num;
while (primeCount != nthPrime) {
num += 2;
if (isPrime (num)) {
primeCount++;
nextPrime = num;
}
}
cout << nextPrime << endl;
}