Sunday, November 16, 2008

Problem #7


#include <iostream>
#include <ctime>
#include <cmath>

using namespace std;

bool isPrime (int);

int main()
{
clock_t start = clock();
int total = 0;
int count = 0;
int count2 = 0;

while (true)
{
if (isPrime (count))
count2++;

if (count2 == 10001)
{
total = count;
break;
}
else
count++;
}

cout << total << endl
<< "Process took " << (static_cast<double> (clock()) - start) / CLOCKS_PER_SEC << " seconds." << endl;

system ("pause");
return 0;
}

bool isPrime (int num)
{
if (num == 0 || num == 1)
return false;

if (num == 2 || num == 3)
return true;

for (unsigned int i = 2; i < static_cast<int> (sqrt (static_cast<double> (num))) + 1; i++)
if (num % i == 0)
return false;
return true;
}

No comments: