Code refractoring.
This commit is contained in:
61
src/global.h
61
src/global.h
@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef VERSION
|
||||
# define VERSION "version"
|
||||
#endif
|
||||
|
||||
/* argp variables */
|
||||
const char *argp_program_version = VERSION;
|
||||
const char *argp_program_bug_address = "<nortega@themusicinnoise.net>";
|
||||
|
||||
/* argp options */
|
||||
static char desc[] = "A parallelized prime number generator.";
|
||||
/*static char args_doc[] = "--count=<number> [--verbose]";*/
|
||||
static struct argp_option opts[] = {
|
||||
{ "count", 'c', "number", 0, "The number of primes to generate (default is 1000)", 0},
|
||||
{ "verbose", 'v', 0, 0, "Print out discovered primes", 0 },
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
struct args {
|
||||
size_t count;
|
||||
int verbose;
|
||||
};
|
||||
|
||||
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
|
||||
struct args *args = state->input;
|
||||
switch(key)
|
||||
{
|
||||
case 'c':
|
||||
args->count = (size_t)atol(arg);
|
||||
break;
|
||||
case 'v':
|
||||
args->verbose = 1;
|
||||
break;
|
||||
case ARGP_KEY_ARG:
|
||||
return 0;
|
||||
default:
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct argp argp = { opts, parse_opt, 0, desc, 0, 0, 0 };
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
* Copyright (C) 2019 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -18,20 +18,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gmp.h>
|
||||
#ifndef APP_NAME
|
||||
#define APP_NAME "app name"
|
||||
#endif
|
||||
|
||||
struct llist_item {
|
||||
mpz_t num;
|
||||
struct llist_item *prev;
|
||||
struct llist_item *next;
|
||||
};
|
||||
|
||||
struct llist {
|
||||
struct llist_item *first;
|
||||
struct llist_item *last;
|
||||
size_t size; // size of the list
|
||||
};
|
||||
|
||||
void llist_init(struct llist *list);
|
||||
int llist_add(struct llist *list, mpz_t num);
|
||||
void llist_deinit(struct llist *list);
|
||||
#ifndef VERSION
|
||||
#define VERSION "version"
|
||||
#endif
|
@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "linked_list.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void llist_init(struct llist *list) {
|
||||
list->size = 0;
|
||||
list->first = NULL;
|
||||
list->last = NULL;
|
||||
}
|
||||
|
||||
int llist_add(struct llist *list, mpz_t num) {
|
||||
struct llist_item *item =
|
||||
malloc(sizeof(struct llist_item));
|
||||
if(!item)
|
||||
return 0;
|
||||
|
||||
mpz_init(item->num);
|
||||
mpz_set(item->num, num);
|
||||
|
||||
if(!(list->first))
|
||||
{
|
||||
list->first = item;
|
||||
list->last = item;
|
||||
}
|
||||
else
|
||||
{
|
||||
list->last->next = item;
|
||||
list->last = item;
|
||||
}
|
||||
list->size++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void llist_deinit(struct llist *list) {
|
||||
struct llist_item *i = list->first;
|
||||
|
||||
while(i)
|
||||
{
|
||||
mpz_clear(i->num);
|
||||
struct llist_item *next = i->next;
|
||||
free(i);
|
||||
i = next;
|
||||
}
|
||||
|
||||
list->size = 0;
|
||||
}
|
91
src/main.c
91
src/main.c
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
* Copyright (C) 2019 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -16,75 +16,56 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <argp.h>
|
||||
#include <gmp.h>
|
||||
|
||||
#include "global.h"
|
||||
#include "linked_list.h"
|
||||
int run = 1;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
struct args args = { 1000, 0 };
|
||||
void quit_signal(int signum) {
|
||||
printf("Received signal %d. Ending process...\n", signum);
|
||||
run = 0;
|
||||
}
|
||||
|
||||
argp_parse(&argp, argc, argv, 0, 0, &args);
|
||||
int main() {
|
||||
printf("%s v%s\n", APP_NAME, VERSION);
|
||||
|
||||
if(args.count == 0)
|
||||
signal(SIGINT, quit_signal);
|
||||
|
||||
mpz_t tnum; // number to be tested for prime-ness
|
||||
mpz_t tsqrt; // square root of tnum
|
||||
mpz_t aux; // auxilary number to test against tnum
|
||||
mpz_t r; // remainder from modulus operation
|
||||
|
||||
mpz_inits(tnum, tsqrt, aux, r, NULL);
|
||||
mpz_set_ui(tnum, 3);
|
||||
mpz_sqrt(tsqrt, tnum);
|
||||
|
||||
while(run)
|
||||
{
|
||||
fprintf(stderr, "ERROR: count must be larger than 0.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct llist prime_list;
|
||||
llist_init(&prime_list);
|
||||
|
||||
{
|
||||
// initialize the list with 2
|
||||
mpz_t tmp;
|
||||
mpz_init(tmp);
|
||||
mpz_set_ui(tmp, 2);
|
||||
llist_add(&prime_list, tmp);
|
||||
mpz_clear(tmp);
|
||||
if(args.verbose)
|
||||
puts("2");
|
||||
}
|
||||
|
||||
mpz_t aux;
|
||||
mpz_init(aux);
|
||||
mpz_set_ui(aux, 3);
|
||||
while(prime_list.size < args.count)
|
||||
{
|
||||
struct llist_item *item = prime_list.first;
|
||||
mpz_set_ui(aux, 3);
|
||||
int is_prime = 1;
|
||||
|
||||
mpz_t root;
|
||||
mpz_init(root);
|
||||
mpz_sqrt(root, aux);
|
||||
while(item && mpz_cmp(item->num, root) < 0)
|
||||
while(mpz_cmp(aux, tsqrt) <= 0)
|
||||
{
|
||||
if(mpz_divisible_p(aux, item->num))
|
||||
{
|
||||
mpz_mod(r, tnum, aux);
|
||||
if(mpz_cmp_ui(r, 0) == 0)
|
||||
is_prime = 0;
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
mpz_add_ui(aux, aux, 2);
|
||||
}
|
||||
|
||||
if(is_prime)
|
||||
{
|
||||
llist_add(&prime_list, aux);
|
||||
if(args.verbose)
|
||||
{
|
||||
mpz_out_str(stdout, 10, aux);
|
||||
printf("\n");
|
||||
}
|
||||
mpz_out_str(stdout, 10, tnum);
|
||||
printf("\n");
|
||||
}
|
||||
mpz_add_ui(aux, aux, 2);
|
||||
|
||||
mpz_add_ui(tnum, tnum, 2);
|
||||
mpz_sqrt(tsqrt, tnum);
|
||||
}
|
||||
|
||||
printf("The %zu prime is ", prime_list.size);
|
||||
mpz_out_str(stdout, 10, prime_list.last->num);
|
||||
printf("\n");
|
||||
mpz_clears(tnum, tsqrt, aux, r, NULL);
|
||||
|
||||
llist_deinit(&prime_list);
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user