2016-12-09 22:02:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
2016-12-14 18:06:33 +00:00
|
|
|
#include <unistd.h>
|
2016-12-09 22:02:51 +00:00
|
|
|
#include <gmp.h>
|
|
|
|
|
2016-12-27 20:31:24 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2016-12-09 22:02:51 +00:00
|
|
|
#include "list.h"
|
2016-12-26 17:50:51 +00:00
|
|
|
#include "files.h"
|
2016-12-09 22:02:51 +00:00
|
|
|
|
2016-12-27 21:52:06 +00:00
|
|
|
#define VERSION "v0.7"
|
2016-12-14 16:52:52 +00:00
|
|
|
|
2016-12-09 22:02:51 +00:00
|
|
|
static bool run;
|
2016-12-14 15:14:58 +00:00
|
|
|
|
2016-12-14 16:52:52 +00:00
|
|
|
void printUsage(char *progName);
|
2016-12-09 22:02:51 +00:00
|
|
|
void leave();
|
|
|
|
|
2016-12-14 16:52:52 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2016-12-14 22:38:22 +00:00
|
|
|
// Variables for argument parsing
|
2016-12-14 18:28:28 +00:00
|
|
|
bool f_help = false,
|
|
|
|
f_version = false,
|
|
|
|
f_quiet = false;
|
2016-12-14 18:06:33 +00:00
|
|
|
int base = 10;
|
2016-12-27 20:31:24 +00:00
|
|
|
char *file = NULL;
|
2016-12-26 17:50:51 +00:00
|
|
|
char *efile = NULL;
|
2016-12-14 16:52:52 +00:00
|
|
|
|
2016-12-14 22:38:22 +00:00
|
|
|
// Parse commandline arguments
|
2016-12-14 16:52:52 +00:00
|
|
|
int c;
|
2016-12-27 20:31:24 +00:00
|
|
|
while((c = getopt(argc, argv, "hvqb:f:e:")) != -1) {
|
2016-12-14 16:52:52 +00:00
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
|
|
|
f_help = true;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
f_version = true;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
f_quiet = true;
|
|
|
|
break;
|
2016-12-14 18:06:33 +00:00
|
|
|
case 'b':
|
|
|
|
base = atoi(optarg);
|
|
|
|
if(base < 2 || base > 62) {
|
|
|
|
fprintf(stderr,
|
2016-12-26 14:56:56 +00:00
|
|
|
"Invalid base `%d'. Base must be between 2 and 62.\n",
|
2016-12-14 18:06:33 +00:00
|
|
|
base);
|
2016-12-27 23:30:31 +00:00
|
|
|
return 1;
|
2016-12-14 18:06:33 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-12-27 20:31:24 +00:00
|
|
|
case 'f':
|
|
|
|
file = optarg;
|
2016-12-14 18:28:28 +00:00
|
|
|
break;
|
2016-12-26 17:50:51 +00:00
|
|
|
case 'e':
|
|
|
|
efile = optarg;
|
|
|
|
break;
|
2016-12-14 16:52:52 +00:00
|
|
|
default:
|
|
|
|
printUsage(argv[0]);
|
2016-12-26 17:50:51 +00:00
|
|
|
return 1;
|
2016-12-14 16:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 22:38:22 +00:00
|
|
|
// Act based on which flags were used
|
2016-12-14 16:52:52 +00:00
|
|
|
if(f_help) {
|
|
|
|
printUsage(argv[0]);
|
2016-12-28 13:43:17 +00:00
|
|
|
puts("\nArguments:");
|
2016-12-14 18:06:33 +00:00
|
|
|
puts(" -h print this help information");
|
|
|
|
puts(" -v print version number of program");
|
|
|
|
puts(" -q quiet mode");
|
2016-12-14 21:26:43 +00:00
|
|
|
puts(" -b <base> base in which to print primes between 2 and 62 (default 10)");
|
2016-12-27 20:31:24 +00:00
|
|
|
puts(" -f <file> file in/from which primes are stored and read from in raw format");
|
2016-12-28 13:43:17 +00:00
|
|
|
puts(" -e <file> export input file to plain text format\n");
|
2016-12-14 16:52:52 +00:00
|
|
|
return 0;
|
|
|
|
} else if(f_version) {
|
|
|
|
printf("Indivisible %s\n", VERSION);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-09 22:02:51 +00:00
|
|
|
|
|
|
|
// Quit on ^C by setting `run = false'
|
|
|
|
run = true;
|
|
|
|
signal(SIGINT, leave);
|
|
|
|
|
2016-12-27 20:31:24 +00:00
|
|
|
if(efile != NULL && file == NULL) {
|
2016-12-26 17:50:51 +00:00
|
|
|
fprintf(stderr, "There must be an input file to export! Use `-h' for help.\n");
|
|
|
|
return 1;
|
2016-12-14 16:52:52 +00:00
|
|
|
}
|
2016-12-27 20:31:24 +00:00
|
|
|
bool newFile = true;
|
|
|
|
if(file != NULL) {
|
|
|
|
struct stat s;
|
|
|
|
if(stat(file, &s) == 0) newFile = false;
|
|
|
|
}
|
2016-12-14 16:52:52 +00:00
|
|
|
|
2016-12-26 17:50:51 +00:00
|
|
|
int exitCode = 0;
|
|
|
|
|
2016-12-09 22:02:51 +00:00
|
|
|
// Primes we've found
|
|
|
|
List primes;
|
2016-12-27 23:30:31 +00:00
|
|
|
if(initList(&primes) == 1) {
|
|
|
|
fprintf(stderr, "Failed to initialize primes list.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-12-09 22:02:51 +00:00
|
|
|
|
|
|
|
// The number we're going to be testing for
|
|
|
|
mpz_t num;
|
|
|
|
mpz_init(num);
|
|
|
|
|
2016-12-26 17:50:51 +00:00
|
|
|
// Variable for half `num'
|
|
|
|
mpz_t halfNum;
|
|
|
|
mpz_init(halfNum);
|
|
|
|
|
|
|
|
if(efile == NULL) puts("Use Ctrl+C to exit.");
|
|
|
|
|
2016-12-27 20:31:24 +00:00
|
|
|
if(newFile) {
|
2016-12-26 14:56:56 +00:00
|
|
|
// Add 2, a known prime to this list
|
|
|
|
mpz_set_ui(num, 2);
|
2016-12-27 23:30:31 +00:00
|
|
|
if(addToList(&primes, num) == 1) {
|
|
|
|
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
|
|
|
goto releaseMemory;
|
|
|
|
}
|
2016-12-26 14:56:56 +00:00
|
|
|
if(!f_quiet) {
|
2016-12-27 23:30:31 +00:00
|
|
|
if(mpz_out_str(stdout, base, num) == 0)
|
2016-12-26 14:56:56 +00:00
|
|
|
fprintf(stderr, "Could not print to `stdout'!\n");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
mpz_add_ui(num, num, 1);
|
|
|
|
} else {
|
|
|
|
// Load primes from file
|
2016-12-27 20:31:24 +00:00
|
|
|
int err = inputPrimes(file, &primes);
|
2016-12-26 17:50:51 +00:00
|
|
|
|
|
|
|
if(err == 0) {
|
|
|
|
printf("Loaded %zu primes.\n", primes.end);
|
|
|
|
} else {
|
|
|
|
if(err == 1)
|
2016-12-27 20:31:24 +00:00
|
|
|
fprintf(stderr, "Failed to open Indivisible file `%s'.\n", file);
|
2016-12-26 17:50:51 +00:00
|
|
|
else if(err == 2)
|
2016-12-27 20:31:24 +00:00
|
|
|
fprintf(stderr, "Failed to close Indivisible file `%s'.\n", file);
|
2016-12-27 23:30:31 +00:00
|
|
|
else if(err == 3)
|
|
|
|
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
2016-12-26 17:50:51 +00:00
|
|
|
exitCode = 1;
|
2016-12-26 14:56:56 +00:00
|
|
|
goto releaseMemory;
|
|
|
|
}
|
2016-12-27 20:47:16 +00:00
|
|
|
/**
|
|
|
|
* Yes, I realize there's a -1 here, I don't know why but it won't
|
|
|
|
* work if it's not there, so don't change it unless necessary.
|
|
|
|
*/
|
|
|
|
mpz_set(num, primes.list[primes.end-1]);
|
2016-12-09 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 17:50:51 +00:00
|
|
|
if(efile != NULL) {
|
|
|
|
int err = exportPrimes(efile, &primes, base);
|
|
|
|
|
|
|
|
if(err == 0) {
|
|
|
|
puts("Successfully exported primes.");
|
|
|
|
} else {
|
|
|
|
if(err == 1)
|
|
|
|
fprintf(stderr, "Failed to open/create plain text file `%s'.\n", efile);
|
|
|
|
else if(err == 2)
|
|
|
|
fprintf(stderr, "Failed to close plain text file `%s'.\n", efile);
|
|
|
|
else if(err == 3)
|
|
|
|
fprintf(stderr, "Failed to write prime to plain text file `%s'.\n", efile);
|
|
|
|
exitCode = 1;
|
|
|
|
}
|
|
|
|
goto releaseMemory;
|
|
|
|
}
|
2016-12-10 13:46:51 +00:00
|
|
|
|
2016-12-09 22:02:51 +00:00
|
|
|
do {
|
2016-12-10 13:46:51 +00:00
|
|
|
// Calculate half of `num'
|
|
|
|
mpz_fdiv_q_ui(halfNum, num, 2);
|
2016-12-14 15:14:58 +00:00
|
|
|
/**
|
|
|
|
* Loop through primes we've found until we get to half of the number
|
|
|
|
* we're analyzing
|
|
|
|
*/
|
2016-12-14 14:55:39 +00:00
|
|
|
for(size_t i = 0; mpz_cmp(primes.list[i], halfNum) < 0; ++i) {
|
2016-12-09 22:02:51 +00:00
|
|
|
// If `num' is divisible by a prime then go to the next number
|
2016-12-10 10:20:01 +00:00
|
|
|
if(mpz_divisible_p(num, primes.list[i]) != 0)
|
|
|
|
goto nextPrime;
|
2016-12-09 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// `num' is a prime so we add it to the list and print it
|
2016-12-27 23:30:31 +00:00
|
|
|
if(addToList(&primes, num) == 1) {
|
|
|
|
fprintf(stderr, "Failed to allocate more memory for list.\n");
|
|
|
|
goto releaseMemory;
|
|
|
|
}
|
2016-12-14 16:52:52 +00:00
|
|
|
if(!f_quiet) {
|
2016-12-27 23:30:31 +00:00
|
|
|
if(mpz_out_str(stdout, base, num) == 0)
|
2016-12-14 16:52:52 +00:00
|
|
|
fprintf(stderr, "Could not print to `stdout'!\n");
|
|
|
|
printf("\n");
|
2016-12-09 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nextPrime:
|
|
|
|
// Add 2 (skip even numbers since they're all divisible by 2)
|
|
|
|
mpz_add_ui(num, num, 2);
|
2016-12-10 13:46:51 +00:00
|
|
|
} while(run);
|
2016-12-09 22:02:51 +00:00
|
|
|
|
2016-12-14 16:31:36 +00:00
|
|
|
printf("Found %zu primes.\n", primes.end);
|
2016-12-14 18:28:28 +00:00
|
|
|
|
2016-12-27 20:31:24 +00:00
|
|
|
if(file != NULL) {
|
|
|
|
int err = outputPrimes(file, &primes);
|
2016-12-26 17:50:51 +00:00
|
|
|
if(err == 0) {
|
|
|
|
puts("Successfully saved primes.");
|
|
|
|
} else {
|
|
|
|
if(err == 1)
|
2016-12-27 20:31:24 +00:00
|
|
|
fprintf(stderr, "Failed to open/create file `%s'.\n", file);
|
2016-12-26 17:50:51 +00:00
|
|
|
else if(err == 2)
|
2016-12-27 20:31:24 +00:00
|
|
|
fprintf(stderr, "Failed to close file `%s'.\n", file);
|
2016-12-26 17:50:51 +00:00
|
|
|
else if(err == 3)
|
2016-12-27 20:31:24 +00:00
|
|
|
fprintf(stderr, "Failed while writing a prime to `%s'.\n", file);
|
2016-12-26 17:50:51 +00:00
|
|
|
exitCode = 1;
|
2016-12-14 18:28:28 +00:00
|
|
|
goto releaseMemory;
|
|
|
|
}
|
2016-12-26 17:50:51 +00:00
|
|
|
|
2016-12-14 18:28:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 13:43:17 +00:00
|
|
|
releaseMemory:
|
|
|
|
puts("Clearing memory...");
|
2016-12-26 17:50:51 +00:00
|
|
|
// Clear GMP variables
|
|
|
|
mpz_clear(halfNum);
|
|
|
|
mpz_clear(num);
|
2016-12-09 22:02:51 +00:00
|
|
|
// Deinitialize the list
|
|
|
|
deInitList(&primes);
|
2016-12-14 14:29:06 +00:00
|
|
|
|
|
|
|
puts("Exit successful.");
|
2016-12-26 17:50:51 +00:00
|
|
|
return exitCode;
|
2016-12-09 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 16:52:52 +00:00
|
|
|
void printUsage(char *progName) {
|
2016-12-28 13:43:17 +00:00
|
|
|
printf("%s [-f <file>] [-q] [-b <base>]\n", progName);
|
|
|
|
printf("%s -f <file> -e <file>\n", progName);
|
2016-12-14 16:52:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 15:14:58 +00:00
|
|
|
void leave() { run = false; }
|