Skip to content

Instantly share code, notes, and snippets.

@romanitalian
Last active December 11, 2020 18:28
Show Gist options
  • Save romanitalian/d25931820f334d7ed60bcf0df5bf104e to your computer and use it in GitHub Desktop.
Save romanitalian/d25931820f334d7ed60bcf0df5bf104e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#include <time.h>
using namespace std;
bool isPrime(int num)
{
if (num == 2) {
return true;
}
if (num <= 1 || num % 2 == 0) {
return false;
}
double sqrt_num = sqrt(double(num));
for (int div = 3; div <= sqrt_num; div +=2)
{
if (num % div == 0) {
return false;
}
}
return true;
}
int main()
{
int N = 10000000;
clock_t start, end;
start = clock();
for (int i = 0; i < N; i++) {
// printf("%d - %d\n", i, isPrime(i));
isPrime(i);
}
end = clock();
cout << (end - start) / ((double) CLOCKS_PER_SEC);
cout << " sec \n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment