/* Simple trial division microbenchmark. */ #include #include int main(int argc, char **argv) { int max; if (argc != 2 || (max = atoi(argv[1])) == 0) { fprintf(stderr, "Usage: %s 53\n" "Prints the last prime number before 53\n", argv[0]); return -1; } if (max <= 2) return 1; if (max == 3) { printf("2\n"); return 0; } for (int p = max % 2 ? max - 2 : max - 1; p > 0; p -= 2) { for (int i = 3; i * i <= p; i += 2) { if (p % i == 0) goto composite; } printf("%d\n", p); return 0; composite:; } return -1; }