#include #include #include #include #include #include #include "list.h" #define VERSION "v1.0" static bool run; void printUsage(char *progName); void leave(); int main(int argc, char *argv[]) { // Variables for argument parsing bool f_help = false, f_version = false, f_quiet = false; int base = 10; char *ofile = NULL; char *ifile = NULL; // Parse commandline arguments int c; while((c = getopt(argc, argv, "hvqb:o:i:")) != -1) { switch(c) { case 'h': f_help = true; break; case 'v': f_version = true; break; case 'q': f_quiet = true; break; case 'b': base = atoi(optarg); if(base < 2 || base > 62) { fprintf(stderr, "Invalid base `%d'. Base must be between 2 and 62.\n", base); exit(1); } break; case 'o': ofile = optarg; break; case 'i': ifile = optarg; break; default: printUsage(argv[0]); exit(1); } } // Act based on which flags were used if(f_help) { printUsage(argv[0]); puts(" -h print this help information"); puts(" -v print version number of program"); puts(" -q quiet mode"); puts(" -b base in which to print primes between 2 and 62 (default 10)"); puts(" -o file to save primes to"); puts(" -i file to read primes from a previous session"); return 0; } else if(f_version) { printf("Indivisible %s\n", VERSION); return 0; } // Quit on ^C by setting `run = false' run = true; signal(SIGINT, leave); if(f_quiet) { puts("Use Ctrl+C (SIGINT) to exit."); } // Primes we've found List primes; initList(&primes); // The number we're going to be testing for mpz_t num; mpz_init(num); if(ifile == NULL) { // Add 2, a known prime to this list mpz_set_ui(num, 2); addToList(&primes, num); if(!f_quiet) { if(mpz_out_str(stdout, base, num) == 0) { fprintf(stderr, "Could not print to `stdout'!\n"); goto releaseMemory; } printf("\n"); } mpz_add_ui(num, num, 1); } else { // Load primes from file FILE *pFile = fopen(ifile, "r"); if(pFile == NULL) { fprintf(stderr, "Failed to open primes file `%s'.\n", ifile); goto releaseMemory; } while(mpz_inp_raw(num, pFile) != 0) { addToList(&primes, num); } if(fclose(pFile) != 0) { fprintf(stderr, "Failed to close file `%s'.\n", ifile); goto releaseMemory; } printf("Loaded %zu primes.\n", primes.end); } // Variable for half `num' mpz_t halfNum; mpz_init(halfNum); do { // Calculate half of `num' mpz_fdiv_q_ui(halfNum, num, 2); /** * Loop through primes we've found until we get to half of the number * we're analyzing */ for(size_t i = 0; mpz_cmp(primes.list[i], halfNum) < 0; ++i) { // If `num' is divisible by a prime then go to the next number if(mpz_divisible_p(num, primes.list[i]) != 0) goto nextPrime; } // `num' is a prime so we add it to the list and print it addToList(&primes, num); if(!f_quiet) { if(mpz_out_str(stdout, base, num) == 0) { fprintf(stderr, "Could not print to `stdout'!\n"); goto releaseMemory; } printf("\n"); } nextPrime: // Add 2 (skip even numbers since they're all divisible by 2) mpz_add_ui(num, num, 2); } while(run); printf("Found %zu primes.\n", primes.end); // Clear GMP variables mpz_clear(halfNum); mpz_clear(num); if(ofile != NULL) { FILE *outFile = fopen(ofile, "w+"); if(outFile == NULL) { fprintf(stderr, "Failed create file `%s'.\n", ofile); goto releaseMemory; } printf("Writing primes to `%s'...\n", ofile); puts("0%"); for(size_t i = 0; i < primes.end; ++i) { if(mpz_out_raw(outFile, primes.list[i]) == 0) { fprintf(stderr, "Error occurred while writing to file `%s'.\n", ofile); goto releaseMemory; } if(i == primes.end / 4) puts("25%"); else if(i == primes.end / 2) puts("50%"); else if(i == primes.end * 3 / 4) puts("75%"); } if(fclose(outFile) != 0) { fprintf(stderr, "Failed to close file `%s'.\n", ofile); goto releaseMemory; } puts("100%"); puts("Finished writing primes."); } releaseMemory: puts("Clearing memory..."); // Deinitialize the list deInitList(&primes); puts("Exit successful."); return 0; } void printUsage(char *progName) { printf("%s [OPTIONS]\n", progName); } void leave() { run = false; }