indivisible-legacy/src/main.c

167 lines
3.5 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>
2016-12-14 18:06:33 +00:00
#include <unistd.h>
#include <gmp.h>
#include "list.h"
2016-12-14 16:52:52 +00:00
#define VERSION "v0.5"
static bool run;
2016-12-14 15:14:58 +00:00
2016-12-14 16:52:52 +00:00
void printUsage(char *progName);
void leave();
2016-12-14 16:52:52 +00:00
int main(int argc, char *argv[]) {
bool f_help = false,
f_version = false,
f_quiet = false;
2016-12-14 18:06:33 +00:00
int base = 10;
char *file = NULL;
2016-12-14 16:52:52 +00:00
int c;
while((c = getopt(argc, argv, "hvqb:f:")) != -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,
"Invalid base `%d'. Base must be between 2 and 62\n",
base);
exit(1);
}
break;
case 'f':
file = optarg;
break;
2016-12-14 16:52:52 +00:00
default:
printUsage(argv[0]);
exit(1);
}
}
if(f_help) {
printUsage(argv[0]);
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");
puts(" -b <base> base in which to print primes between 2 and 62");
puts(" -f <file> file to save primes to");
2016-12-14 16:52:52 +00:00
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);
2016-12-14 16:52:52 +00:00
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);
// Add 2, a known prime to this list
mpz_set_ui(num, 2);
addToList(&primes, num);
2016-12-14 16:52:52 +00:00
if(!f_quiet) {
2016-12-14 18:06:33 +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");
goto releaseMemory;
2016-12-14 16:52:52 +00:00
}
printf("\n");
}
mpz_add_ui(num, num, 1);
// Variable for half `num'
mpz_t halfNum;
mpz_init(halfNum);
do {
// 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) {
// 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;
}
// `num' is a prime so we add it to the list and print it
addToList(&primes, num);
2016-12-14 16:52:52 +00:00
if(!f_quiet) {
2016-12-14 18:06:33 +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");
goto releaseMemory;
2016-12-14 16:52:52 +00:00
}
printf("\n");
}
nextPrime:
// Add 2 (skip even numbers since they're all divisible by 2)
mpz_add_ui(num, num, 2);
} while(run);
2016-12-14 16:31:36 +00:00
printf("Found %zu primes.\n", primes.end);
// Clear GMP variables
mpz_clear(halfNum);
2016-12-10 10:20:01 +00:00
mpz_clear(num);
if(file != NULL) {
FILE *outFile = fopen(file, "w");
if(outFile == NULL) {
fprintf(stderr, "Failed create file `%s'.\n", file);
goto releaseMemory;
}
printf("Writing primes to `%s'...\n", file);
for(size_t i = 0; i < primes.end; ++i) {
if(mpz_out_str(outFile, base, primes.list[i]) == 0) {
fprintf(stderr, "Error occurred while writing to file `%s'.\n", file);
goto releaseMemory;
}
fprintf(outFile, "\n");
}
if(fclose(outFile) != 0) {
fprintf(stderr, "Failed to close file `%s'.\n", file);
goto releaseMemory;
}
puts("Finished writing primes.");
}
releaseMemory:
puts("Clearing memory...");
// Deinitialize the list
deInitList(&primes);
2016-12-14 14:29:06 +00:00
puts("Exit successful.");
return 0;
}
2016-12-14 16:52:52 +00:00
void printUsage(char *progName) {
2016-12-14 18:06:33 +00:00
printf("%s [options...]\n", progName);
2016-12-14 16:52:52 +00:00
}
2016-12-14 15:14:58 +00:00
void leave() { run = false; }