Last active
December 11, 2020 18:28
-
-
Save romanitalian/d25931820f334d7ed60bcf0df5bf104e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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