Hello, guys.
This challenge asks us to find the 10001-th prime. Here's my code:
// A reasonable approximation is to say that the n-th prime is such that p(n) = n*(log n + log*(log n) - 1).
// With that in mind, we can say that the 10001-th prime is smaller than 10100*(log 10100 + log(log 10100) - 1), which we can round to 105000.
#include <iostream>
#include <cmath>
#define MAX_INTERVAL 105000
using namespace std;
int Wanted_Prime = 0;
void Show_Prime(int);
void Show_Prime(int Wanted_Prime)
{
cout << "The 10001-th prime is " << Wanted_Prime << endl;
}
int main()
{
bool Is_Prime = true;
int Number_of_Primes = 1;
for (int i = 3; i < MAX_INTERVAL; i += 2)
{
Is_Prime = true;
for (int j =2; j <= floor(sqrt(i)); j++)
{
if (i % j == 0)
{
Is_Prime = false;
}
}
if (Is_Prime == true)
{
Number_of_Primes++;
}
if (Number_of_Primes == 10001)
{
Wanted_Prime = i;
Show_Prime(Wanted_Prime);
i = MAX_INTERVAL;
}
}
return 0;
}
It takes 0.300s to run. What can I do to improve it?
Thanks.
Petcheco