-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0007_10001st_prime.cpp
More file actions
60 lines (52 loc) · 1.64 KB
/
0007_10001st_prime.cpp
File metadata and controls
60 lines (52 loc) · 1.64 KB
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
57
58
59
60
/**********************
Author: Daniel Bowder
Date: July 21, 2018
Problem: https://projecteuler.net/problem=7
"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 <cmath>
using std::cout;
using std::endl;
bool isPrime(double i) {
// Iterate through numbers less than or equal to half of the value querried for prime.
for (double ii=2; ii <= i/2; ii++) {
// If any number we iterate through is a factor of the original number,
if (0 == fmod(i, ii)) {
// then the original number is not prime, return false.
return false;
}
}
// If we've checked all of the numbers up to 1/2 of the value querried and found no
// factors, then this number is prime. Return true.
return true;
}
double nthPrime(double value) {
// Prepare placeholder for the current prime
int currentPrime = 0;
// Count the number of primes we've found
int counter = 0;
// Index our current number to be tested for prime-ness
int x = 0;
// While we haven't found enough primes yet,
while (counter <= value + 1) {
// Check if our x index is prime. If it is,
if (isPrime(x)) {
// set our current prime,
currentPrime = x;
// incriment x,
x++;
// and incriment our prime counter.
counter++;
} else {
// If this nubmer is not prime, incriment x and continue.
x++;
}
}
// When we've counted enough prime numbers to equal our nth prime,
return currentPrime;
}
int main(int argc, char* argv[]) {
cout << "Problem 7. " << nthPrime(10001) << endl;
}