Compare commits
No commits in common. "23b8d988e838041cabdbcb1a77088f3729b3be38" and "874cc75431fb8ade1ca7c154bc8ddff47e89f502" have entirely different histories.
23b8d988e8
...
874cc75431
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,6 +1,6 @@
|
|||||||
# Ignore binary files
|
# Ignore build files
|
||||||
*.o
|
build/*
|
||||||
indivisible
|
!build/.keep
|
||||||
|
|
||||||
# Ignore vim temporary files
|
# Ignore vim temporary files
|
||||||
*.sw[a-z]
|
*.sw[a-z]
|
||||||
|
@ -37,6 +37,6 @@ Change Log
|
|||||||
- Allow searching for the nth prime number
|
- Allow searching for the nth prime number
|
||||||
- Clearing some memory leaks
|
- Clearing some memory leaks
|
||||||
- v2.0: Linked List
|
- v2.0: Linked List
|
||||||
|
- Implement linked list for faster addition to list.
|
||||||
|
- Multi-Processor support (MPI).
|
||||||
- Code cleanup.
|
- Code cleanup.
|
||||||
- Replace OpenMP code with OpenMPI.
|
|
||||||
- Use linked list for prime storage.
|
|
||||||
|
66
CMakeLists.txt
Normal file
66
CMakeLists.txt
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
# Copyright (C) 2017 Ortega Froysa, Nicolás <deathsbreed@themusicinnoise.net>
|
||||||
|
# Author: Ortega Froysa, Nicolás <deathsbreed@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/>.
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.0)
|
||||||
|
project(Indivisible C)
|
||||||
|
|
||||||
|
set(TARGET_NAME indivisible)
|
||||||
|
set(TARGET_VERSION "v2.0")
|
||||||
|
|
||||||
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
|
set(CMAKE_BUILD_TYPE "release")
|
||||||
|
endif()
|
||||||
|
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
|
||||||
|
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
|
||||||
|
|
||||||
|
set(CMAKE_MODULE_PATH
|
||||||
|
${CMAKE_MODULE_PATH}
|
||||||
|
${CMAKE_SOURCE_DIR}/cmake/)
|
||||||
|
|
||||||
|
find_package(GMP REQUIRED)
|
||||||
|
find_package(MPI REQUIRED)
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
${MPI_C_INCLUDE_PATH}
|
||||||
|
${GMP_INCLUDE_DIR})
|
||||||
|
|
||||||
|
set(SRCS
|
||||||
|
"src/linked_list.c"
|
||||||
|
"src/main.c")
|
||||||
|
|
||||||
|
# Define the C flags.
|
||||||
|
set(CMAKE_C_FLAGS "-std=gnu99 ${MPI_C_COMPILE_FLAGS} -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
|
||||||
|
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
|
||||||
|
set(CMAKE_C_FLAGS_RELEASE "-O3")
|
||||||
|
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3")
|
||||||
|
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
|
||||||
|
|
||||||
|
if(NOT CMAKE_BUILD_TYPE MATCHES "debug" AND NOT CMAKE_BUILD_TYPE MATCHES "relwithdebinfo")
|
||||||
|
add_definitions("-DNDEBUG")
|
||||||
|
else()
|
||||||
|
add_definitions("-DDEBUG")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_definitions("-DVERSION=\"${TARGET_VERSION}\"")
|
||||||
|
|
||||||
|
add_executable(${TARGET_NAME} ${SRCS})
|
||||||
|
|
||||||
|
target_link_libraries(${TARGET_NAME}
|
||||||
|
${MPI_C_LIBRARIES}
|
||||||
|
${GMP_LIBRARY})
|
||||||
|
|
||||||
|
install(TARGETS ${TARGET_NAME}
|
||||||
|
RUNTIME DESTINATION "bin")
|
6
LICENSE
6
LICENSE
@ -631,8 +631,8 @@ to attach them to the start of each source file to most effectively
|
|||||||
state the exclusion of warranty; and each file should have at least
|
state the exclusion of warranty; and each file should have at least
|
||||||
the "copyright" line and a pointer to where the full notice is found.
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
<one line to give the program's name and a brief idea of what it does.>
|
{one line to give the program's name and a brief idea of what it does.}
|
||||||
Copyright (C) <year> <name of author>
|
Copyright (C) {year} {name of author}
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail.
|
|||||||
If the program does terminal interaction, make it output a short
|
If the program does terminal interaction, make it output a short
|
||||||
notice like this when it starts in an interactive mode:
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
<program> Copyright (C) <year> <name of author>
|
{project} Copyright (C) {year} {fullname}
|
||||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
This is free software, and you are welcome to redistribute it
|
This is free software, and you are welcome to redistribute it
|
||||||
under certain conditions; type `show c' for details.
|
under certain conditions; type `show c' for details.
|
||||||
|
48
Makefile
48
Makefile
@ -1,48 +0,0 @@
|
|||||||
# 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
|
|
||||||
# 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/>.
|
|
||||||
|
|
||||||
CC=mpicc
|
|
||||||
DEBUG=0
|
|
||||||
PREFIX=/usr/local
|
|
||||||
INCFLAGS=
|
|
||||||
LDFLAGS=-lgmp
|
|
||||||
DEFS=-DVERSION=\"2.0\" -DAPP_NAME=\"Indivisible\"
|
|
||||||
CFLAGS=$(INCFLAGS) $(DEFS) -std=c99 -Wall -Wextra -Wfatal-errors -Werror
|
|
||||||
HDRS=src/globals.h src/llist.h
|
|
||||||
OBJS=src/llist.o src/main.o src/prime_test.o
|
|
||||||
|
|
||||||
ifeq ($(DEBUG),1)
|
|
||||||
CFLAGS+=-g -O0 -DDEBUG
|
|
||||||
else
|
|
||||||
CFLAGS+=-O2 -DNDEBUG
|
|
||||||
endif
|
|
||||||
|
|
||||||
%.o:%.c $(HDRS)
|
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
|
||||||
|
|
||||||
indivisible: $(OBJS)
|
|
||||||
$(CC) -o $@ $^ $(LDFLAGS)
|
|
||||||
|
|
||||||
.PHONY: clean distclean install
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) $(OBJS)
|
|
||||||
|
|
||||||
distclean: clean
|
|
||||||
$(RM) indivisible
|
|
||||||
|
|
||||||
install: indivisible
|
|
||||||
install -m 755 indivisible $(PREFIX)/bin/
|
|
40
README
40
README
@ -1,34 +1,32 @@
|
|||||||
===================
|
===================
|
||||||
*** Indivisible ***
|
*** Indivisible ***
|
||||||
===================
|
===================
|
||||||
A parallelized prime number generator written in C. It uses a bignum
|
Indivisible is a parallelized prime number generator written in C.
|
||||||
library as well as OpenMPI for parallelization.
|
|
||||||
|
|
||||||
# Building
|
# Building
|
||||||
----------
|
----------
|
||||||
## Dependencies
|
There are multiple dependencies to install before compiling the project:
|
||||||
The dependencies for compiling are minimal, and should be available
|
|
||||||
on most UNIX-like systems:
|
|
||||||
- GNU Make
|
|
||||||
- GNU Multi-Precision Arithmetics Library (GMP)
|
|
||||||
- OpenMPI
|
|
||||||
|
|
||||||
## Compiling
|
- CMake build system
|
||||||
To compile you simply need to run the `make` command, which will
|
- GNU Multi-Precision Arithmetics Library
|
||||||
create the `indivisible` binary file. Additionally you can set the
|
|
||||||
following flags by appending them to the `make` command:
|
Once the dependencies are installed you can compile by running the following
|
||||||
- `DEBUG=<1|0>`: whether to create a debug or release build. (0 by
|
from the root directory of the project:
|
||||||
default)
|
|
||||||
- `PREFIX=<path>`: what base prefix to install the binaries at.
|
cd build/
|
||||||
(`/usr/local` by default)
|
cmake ..
|
||||||
|
make
|
||||||
|
|
||||||
|
This will create a release build, to build with debug options append the
|
||||||
|
`-DCMAKE_BUILD_TYPE=debug' flag to the `cmake' command.
|
||||||
|
|
||||||
# Contributing
|
# Contributing
|
||||||
--------------
|
--------------
|
||||||
To contribute to the project, please follow the same coding style you
|
To contribute to indivisible you can send patch files to my e-mail[0].
|
||||||
see in the current code base. Then, either submit a pull request or send
|
|
||||||
a patch file to <nicolas@ortegas.org>.
|
[0] nortega@themusicinnoise.net
|
||||||
|
|
||||||
# License
|
# License
|
||||||
---------
|
---------
|
||||||
This project is licensed under the terms & conditions of the GNU General
|
This project is licensed under the GNU General Public License version 3 or
|
||||||
Public License version 3 or greater (see `LICENSE` file).
|
greater (see `LICENSE' for more information).
|
||||||
|
0
build/.keep
Normal file
0
build/.keep
Normal file
24
cmake/FindGMP.cmake
Normal file
24
cmake/FindGMP.cmake
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# SOURCE: http://stackoverflow.com/questions/29307862/error-linking-gmp-library
|
||||||
|
set(GMP_PREFIX "" CACHE PATH "path ")
|
||||||
|
|
||||||
|
find_path(GMP_INCLUDE_DIR gmp.h gmpxx.h
|
||||||
|
PATHS ${GMP_PREFIX}/include /usr/include /usr/local/include )
|
||||||
|
|
||||||
|
find_library(GMP_LIBRARY NAMES gmp libgmp
|
||||||
|
PATHS ${GMP_PREFIX}/lib /usr/lib /usr/local/lib)
|
||||||
|
|
||||||
|
|
||||||
|
if(GMP_INCLUDE_DIR AND GMP_LIBRARY)
|
||||||
|
get_filename_component(GMP_LIBRARY_DIR ${GMP_LIBRARY} PATH)
|
||||||
|
set(GMP_FOUND TRUE)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(GMP_FOUND)
|
||||||
|
if(NOT GMP_FIND_QUIETLY)
|
||||||
|
MESSAGE(STATUS "Found GMP: ${GMP_LIBRARY}")
|
||||||
|
endif()
|
||||||
|
else()
|
||||||
|
if(GMP_FIND_REQUIRED)
|
||||||
|
message(FATAL_ERROR "Could not find GMP")
|
||||||
|
endif()
|
||||||
|
endif()
|
61
src/global.h
Normal file
61
src/global.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* 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,27 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
* 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 APP_NAME
|
|
||||||
#define APP_NAME "app name"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef VERSION
|
|
||||||
#define VERSION "version"
|
|
||||||
#endif
|
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2019 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||||
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -16,46 +16,50 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "llist.h"
|
#include "linked_list.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
void llist_init(struct llist *list) {
|
void llist_init(struct llist *list) {
|
||||||
|
list->size = 0;
|
||||||
list->first = NULL;
|
list->first = NULL;
|
||||||
list->last = NULL;
|
list->last = NULL;
|
||||||
list->size = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void llist_deinit(struct llist *list) {
|
int llist_add(struct llist *list, mpz_t num) {
|
||||||
struct llist_item *aux = list->first;
|
struct llist_item *item =
|
||||||
while(aux)
|
|
||||||
{
|
|
||||||
mpz_clear(aux->n);
|
|
||||||
struct llist_item *tmp = aux;
|
|
||||||
aux = aux->next;
|
|
||||||
free(tmp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int llist_insert(struct llist *list, mpz_t n) {
|
|
||||||
struct llist_item *aux =
|
|
||||||
malloc(sizeof(struct llist_item));
|
malloc(sizeof(struct llist_item));
|
||||||
if(!aux)
|
if(!item)
|
||||||
{
|
|
||||||
fprintf(stderr, "ERROR llist_insert(): failed to allocate memory");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
aux->prev = list->last;
|
|
||||||
aux->next = NULL;
|
|
||||||
// TODO: check time complexity of these GMP functions
|
|
||||||
mpz_init(aux->n);
|
|
||||||
mpz_set(aux->n, n);
|
|
||||||
|
|
||||||
if(!list->first)
|
mpz_init(item->num);
|
||||||
list->first = aux;
|
mpz_set(item->num, num);
|
||||||
list->last = aux;
|
|
||||||
|
if(!(list->first))
|
||||||
|
{
|
||||||
|
list->first = item;
|
||||||
|
list->last = item;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
list->last->next = item;
|
||||||
|
list->last = item;
|
||||||
|
}
|
||||||
list->size++;
|
list->size++;
|
||||||
|
|
||||||
return 1;
|
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;
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2019 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||||
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -20,8 +20,18 @@
|
|||||||
|
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
|
|
||||||
/*
|
struct llist_item {
|
||||||
* checks to see if the number `p` is prime, returning 1 if it is and 0
|
mpz_t num;
|
||||||
* if it is not.
|
struct llist_item *prev;
|
||||||
*/
|
struct llist_item *next;
|
||||||
int test_prime(const mpz_t p);
|
};
|
||||||
|
|
||||||
|
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);
|
57
src/llist.h
57
src/llist.h
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
* 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
|
|
||||||
|
|
||||||
#include <gmp.h>
|
|
||||||
|
|
||||||
struct llist_item {
|
|
||||||
mpz_t n; // the number stored
|
|
||||||
struct llist_item *next; // the next element in the list
|
|
||||||
struct llist_item *prev; // the previous element in the list
|
|
||||||
};
|
|
||||||
|
|
||||||
struct llist {
|
|
||||||
struct llist_item *first; // first element of the list
|
|
||||||
struct llist_item *last; // last element of the list
|
|
||||||
size_t size; // number of elements in the list
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* initialize an empty linked list where `first` and `last` are
|
|
||||||
* equal to NULL and `size` is 0.
|
|
||||||
*/
|
|
||||||
void llist_init(struct llist *list);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* free all space allocated to the list.
|
|
||||||
*/
|
|
||||||
void llist_deinit(struct llist *list);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* insert an item at the end of the linked list with value `n`.
|
|
||||||
*
|
|
||||||
* returns 1 on success, 0 on failure.
|
|
||||||
*/
|
|
||||||
int llist_insert(struct llist *list, mpz_t n);
|
|
||||||
/*
|
|
||||||
* insert an item in the linked list in a sorted position.
|
|
||||||
*
|
|
||||||
* returns 1 on success, 0 on failure.
|
|
||||||
*/
|
|
||||||
int llist_sorted_insert(struct llist *list, mpz_t n);
|
|
90
src/main.c
90
src/main.c
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2019 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||||
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -16,55 +16,75 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "globals.h"
|
|
||||||
#include "llist.h"
|
|
||||||
#include "prime_test.h"
|
|
||||||
|
|
||||||
#include <signal.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <argp.h>
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
|
|
||||||
int run = 1;
|
#include "global.h"
|
||||||
|
#include "linked_list.h"
|
||||||
|
|
||||||
void quit_signal(int signum) {
|
int main(int argc, char *argv[]) {
|
||||||
printf("Received signal %d. Ending process...\n", signum);
|
struct args args = { 1000, 0 };
|
||||||
run = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
argp_parse(&argp, argc, argv, 0, 0, &args);
|
||||||
printf("%s v%s\n", APP_NAME, VERSION);
|
|
||||||
|
|
||||||
signal(SIGINT, quit_signal);
|
if(args.count == 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: count must be larger than 0.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
mpz_t test_base; // number used to get tested numbers
|
struct llist prime_list;
|
||||||
mpz_t p0, p1; // numbers to be tested for prime-ness.
|
llist_init(&prime_list);
|
||||||
|
|
||||||
mpz_inits(test_base, p0, p1, NULL);
|
|
||||||
mpz_set_ui(test_base, 6); // test_base = 6*k
|
|
||||||
|
|
||||||
|
{
|
||||||
|
// 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");
|
puts("2");
|
||||||
puts("3");
|
}
|
||||||
|
|
||||||
while(run)
|
mpz_t aux;
|
||||||
|
mpz_init(aux);
|
||||||
|
mpz_set_ui(aux, 3);
|
||||||
|
while(prime_list.size < args.count)
|
||||||
{
|
{
|
||||||
mpz_sub_ui(p0, test_base, 1); // p0 = test_base - 1
|
struct llist_item *item = prime_list.first;
|
||||||
mpz_add_ui(p1, test_base, 1); // p0 = test_base + 1
|
int is_prime = 1;
|
||||||
|
|
||||||
if(test_prime(p0))
|
mpz_t root;
|
||||||
|
mpz_init(root);
|
||||||
|
mpz_sqrt(root, aux);
|
||||||
|
while(item && mpz_cmp(item->num, root) < 0)
|
||||||
{
|
{
|
||||||
mpz_out_str(stdout, 10, p0);
|
if(mpz_divisible_p(aux, item->num))
|
||||||
|
{
|
||||||
|
is_prime = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
item = item->next;
|
||||||
|
}
|
||||||
|
if(is_prime)
|
||||||
|
{
|
||||||
|
llist_add(&prime_list, aux);
|
||||||
|
if(args.verbose)
|
||||||
|
{
|
||||||
|
mpz_out_str(stdout, 10, aux);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
if(test_prime(p1))
|
}
|
||||||
{
|
mpz_add_ui(aux, aux, 2);
|
||||||
mpz_out_str(stdout, 10, p1);
|
}
|
||||||
|
|
||||||
|
printf("The %zu prime is ", prime_list.size);
|
||||||
|
mpz_out_str(stdout, 10, prime_list.last->num);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
|
||||||
|
|
||||||
mpz_add_ui(test_base, test_base, 6); // k += 1
|
|
||||||
}
|
|
||||||
|
|
||||||
mpz_clears(test_base, p0, p1, NULL);
|
|
||||||
|
|
||||||
|
llist_deinit(&prime_list);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
* 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 "prime_test.h"
|
|
||||||
|
|
||||||
int test_prime(const mpz_t p) {
|
|
||||||
mpz_t psqrt;
|
|
||||||
mpz_t aux;
|
|
||||||
mpz_inits(psqrt, aux, NULL);
|
|
||||||
mpz_sqrt(psqrt, p);
|
|
||||||
int is_prime = 1;
|
|
||||||
for(mpz_set_ui(aux, 5); mpz_cmp(aux, psqrt) <= 0; mpz_add_ui(aux, aux, 2))
|
|
||||||
{
|
|
||||||
if(mpz_divisible_p(p, aux))
|
|
||||||
{
|
|
||||||
is_prime = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mpz_clears(psqrt, aux, NULL);
|
|
||||||
|
|
||||||
return is_prime;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user