Quote: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?
Try to solve it by yourself and then look at the solution.
Solution:
Spoiler (Click to View)
Quote: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>
bool IsPrime(int num)
{
if(num % 2 == 0 && num != 2)
return false;
for(int i = 3; i <= sqrt((long double)num); i += 2)
if(num % i == 0)
return false;
return true;
}
int main()
{
unsigned long i = 2, n;
std::cout << "Find the nth prime number: ";
std::cin >> n;
for(int j = 0; j < n; i++)
{
if(IsPrime(i))
j++;
}
std::cout << i - 1;
std::cin.ignore();
std::cin.get();
return 0;
}
No comments:
Post a Comment