BUPT-homework/semester3/exercises/2-Algorighm-Analysis/exercise2.13.c

31 lines
702 B
C
Raw Permalink Normal View History

2022-09-12 16:56:48 +08:00
#include <math.h>
#include <stdio.h>
#include <time.h>
void printTime(clock_t end, clock_t start) {
printf("Took %lf to calculate.\n", ((double)(end - start)) / CLOCKS_PER_SEC);
}
void calculate(long long N) {
clock_t end, start;
start = clock();
for (int i = 1, root = (int)floor(sqrt((double)N)); i < root; i++) {
if (N % i == 0) {
end = clock();
printTime(end, start);
printf("%lld is not prime\n", N);
return;
}
}
end = clock();
printTime(end, start);
printf("%lld is prime\n", N);
return;
}
int main(void) {
long long test[] = {1048575, 1099511627775};
for (int i = 0; i < 2; i++) {
calculate(test[i]);
printf("\n");
}
return 0;
}