Last active
April 11, 2018 09:45
-
-
Save kylethedeveloper/3f27534f908e9ba3cd826bfa0b5b1cf5 to your computer and use it in GitHub Desktop.
Project Euler - Problem 7 - 10001st prime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Project_7.cpp : 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 10 001st prime number? | |
*/ | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int counter = 1, n=3; // I started counter from 1 as I passed 2. | |
while (true) { | |
for (int i = 2; i <= (n/2); i++) // I do until (n/2) as greater values are unnecessary | |
{ | |
if (n % i == 0) { | |
counter--; | |
break; | |
} | |
} | |
counter++; | |
if (counter == 10001) | |
break; | |
else | |
n += 2; // increment with 2, as only odd numbers are prime | |
} | |
cout << "10 001st prime number is " << n << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment