/* program which finds the prime factorization of any number */ #include #include int is_prime(unsigned long long value) { unsigned long long i; if (value == 2) { return 1; } /* look for a number that divides it evenly */ for (i = 2; i <= ceil(sqrt(value)); i++) { if ((value % i) == 0) { return 0; } } return 1; } /* print the prime factors of a number and return whether it has factors or not */ int factorize(unsigned long long value) { /* copy of the value we can modify */ unsigned long long temp = value, i; int composite = 0; /* try all numbers up to half this value */ for (i = 2; i <= ceil(value / 2.0); i++) { /* if the number is prime, consider it */ if (is_prime(i)) { /* each prime factor may be in there multiple times, get them all */ while ((temp % i) == 0) { printf("\t%llu\n", i); composite = 1; temp /= i; } } } return composite; } int main(int argc, char** argv) { /* get input */ if (argc < 2) { printf("Pass a number!\n"); return 0; } unsigned long long value; sscanf(argv[1], "%llu", &value); /* get factors, unless prime */ int comp = factorize(value); if (!comp) { printf("The number is prime."); } return 0; }