#include <iostream>
#include <iomanip>
#include <cmath>
#define START 1
#define STOP 10000000
#define PRECISION 10
using namespace std;
bool myPrimeFunc(unsigned n){
unsigned end = sqrt(n);
if (n%2 == 0 || n%3 == 0 || n%5 == 0 || n%7 == 0)
return false;
for(unsigned i = 11; i <= end; i += 2 )
if(n%i == 0)
return false;
return true;
}
int main () {
float totalTime = 0;
for(int n = 0; n < PRECISION; n++){
int startTime = clock();
int primeCount = 0;
cout << "Initiated Prime Search.." << endl;
for(unsigned i = START; i < STOP; i++ ){
if(i == 2 || i == 3 || i == 5 || i == 7)
primeCount++;
else if(myPrimeFunc(i)){
//cout << i << " is a prime."<< endl; //print each result
primeCount++;
}
}
cout << "Found a total of: " << primeCount << " Prime Numbers." << endl;
cout << "Ended in " << setprecision(9) << fixed << (float)(clock() - startTime)/CLOCKS_PER_SEC << " seconds." << endl;
totalTime += (float)(clock() - startTime)/CLOCKS_PER_SEC;
}
cout << "\nTotal time: " << totalTime;
cout << "\nAverage time: " << totalTime/PRECISION;
return 0;
}