#include <stdio.h> #include <stdbool.h> #include <inttypes.h> #include <math.h> bool is_prime(uint64_t n) { if (n <= 1) return false; if (n <= 3) return true; if ((n % 2 == 0) || (n % 3 == 0)) return false; uint64_t sqrt_n = sqrt(n); for (uint64_t i = 5; i <= sqrt_n; i += 6) { if ((n % i == 0) || (n % (i + 2) == 0)) { return false; } } return true; } int main(int argc, char **argv) { if (argc < 2) { fputs("Pass a positive integer as an argument.\n", stderr); return 1; } uint64_t input; if ((argv[1][0] == '-') || (sscanf(argv[1], "%" SCNu64, &input) != 1)) { fputs("This is not a positive integer.\n", stderr); return 1; } printf("This integer %s prime.\n", is_prime(input) ? "is" : "is not"); return 0; }