Compare commits
10 Commits
874cc75431
...
23b8d988e8
Author | SHA1 | Date | |
---|---|---|---|
|
23b8d988e8 | ||
|
84687f86c1 | ||
|
2cfc24afb7 | ||
|
d61ef9b034 | ||
|
23592a98ed | ||
|
fadcafaeaf | ||
|
63e2eda663 | ||
|
d5fb2b06df | ||
|
68ac85f3f0 | ||
|
4d470a14de |
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,6 +1,6 @@
|
||||
# Ignore build files
|
||||
build/*
|
||||
!build/.keep
|
||||
# Ignore binary files
|
||||
*.o
|
||||
indivisible
|
||||
|
||||
# Ignore vim temporary files
|
||||
*.sw[a-z]
|
||||
|
@ -37,6 +37,6 @@ Change Log
|
||||
- Allow searching for the nth prime number
|
||||
- Clearing some memory leaks
|
||||
- v2.0: Linked List
|
||||
- Implement linked list for faster addition to list.
|
||||
- Multi-Processor support (MPI).
|
||||
- Code cleanup.
|
||||
- Replace OpenMP code with OpenMPI.
|
||||
- Use linked list for prime storage.
|
||||
|
@ -1,66 +0,0 @@
|
||||
# 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
|
||||
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.}
|
||||
Copyright (C) {year} {name of author}
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
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
|
||||
@ -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
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
{project} Copyright (C) {year} {fullname}
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
48
Makefile
Normal file
48
Makefile
Normal file
@ -0,0 +1,48 @@
|
||||
# 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,32 +1,34 @@
|
||||
===================
|
||||
*** Indivisible ***
|
||||
===================
|
||||
Indivisible is a parallelized prime number generator written in C.
|
||||
A parallelized prime number generator written in C. It uses a bignum
|
||||
library as well as OpenMPI for parallelization.
|
||||
|
||||
# Building
|
||||
----------
|
||||
There are multiple dependencies to install before compiling the project:
|
||||
## Dependencies
|
||||
The dependencies for compiling are minimal, and should be available
|
||||
on most UNIX-like systems:
|
||||
- GNU Make
|
||||
- GNU Multi-Precision Arithmetics Library (GMP)
|
||||
- OpenMPI
|
||||
|
||||
- CMake build system
|
||||
- GNU Multi-Precision Arithmetics Library
|
||||
|
||||
Once the dependencies are installed you can compile by running the following
|
||||
from the root directory of the project:
|
||||
|
||||
cd build/
|
||||
cmake ..
|
||||
make
|
||||
|
||||
This will create a release build, to build with debug options append the
|
||||
`-DCMAKE_BUILD_TYPE=debug' flag to the `cmake' command.
|
||||
## Compiling
|
||||
To compile you simply need to run the `make` command, which will
|
||||
create the `indivisible` binary file. Additionally you can set the
|
||||
following flags by appending them to the `make` command:
|
||||
- `DEBUG=<1|0>`: whether to create a debug or release build. (0 by
|
||||
default)
|
||||
- `PREFIX=<path>`: what base prefix to install the binaries at.
|
||||
(`/usr/local` by default)
|
||||
|
||||
# Contributing
|
||||
--------------
|
||||
To contribute to indivisible you can send patch files to my e-mail[0].
|
||||
|
||||
[0] nortega@themusicinnoise.net
|
||||
To contribute to the project, please follow the same coding style you
|
||||
see in the current code base. Then, either submit a pull request or send
|
||||
a patch file to <nicolas@ortegas.org>.
|
||||
|
||||
# License
|
||||
---------
|
||||
This project is licensed under the GNU General Public License version 3 or
|
||||
greater (see `LICENSE' for more information).
|
||||
This project is licensed under the terms & conditions of the GNU General
|
||||
Public License version 3 or greater (see `LICENSE` file).
|
||||
|
@ -1,24 +0,0 @@
|
||||
# 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
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 };
|
27
src/globals.h
Normal file
27
src/globals.h
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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) 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,50 +16,46 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "linked_list.h"
|
||||
#include "llist.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void llist_init(struct llist *list) {
|
||||
list->size = 0;
|
||||
list->first = NULL;
|
||||
list->last = NULL;
|
||||
list->size = 0;
|
||||
}
|
||||
|
||||
int llist_add(struct llist *list, mpz_t num) {
|
||||
struct llist_item *item =
|
||||
void llist_deinit(struct llist *list) {
|
||||
struct llist_item *aux = list->first;
|
||||
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));
|
||||
if(!item)
|
||||
if(!aux)
|
||||
{
|
||||
fprintf(stderr, "ERROR llist_insert(): failed to allocate memory");
|
||||
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;
|
||||
}
|
||||
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)
|
||||
list->first = aux;
|
||||
list->last = aux;
|
||||
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;
|
||||
}
|
57
src/llist.h
Normal file
57
src/llist.h
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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) 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,55 @@
|
||||
* 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 <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)
|
||||
{
|
||||
fprintf(stderr, "ERROR: count must be larger than 0.\n");
|
||||
return 1;
|
||||
}
|
||||
signal(SIGINT, quit_signal);
|
||||
|
||||
struct llist prime_list;
|
||||
llist_init(&prime_list);
|
||||
mpz_t test_base; // number used to get tested numbers
|
||||
mpz_t p0, p1; // numbers to be tested for prime-ness.
|
||||
|
||||
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("3");
|
||||
|
||||
mpz_t aux;
|
||||
mpz_init(aux);
|
||||
mpz_set_ui(aux, 3);
|
||||
while(prime_list.size < args.count)
|
||||
while(run)
|
||||
{
|
||||
struct llist_item *item = prime_list.first;
|
||||
int is_prime = 1;
|
||||
mpz_sub_ui(p0, test_base, 1); // p0 = test_base - 1
|
||||
mpz_add_ui(p1, test_base, 1); // p0 = test_base + 1
|
||||
|
||||
mpz_t root;
|
||||
mpz_init(root);
|
||||
mpz_sqrt(root, aux);
|
||||
while(item && mpz_cmp(item->num, root) < 0)
|
||||
if(test_prime(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);
|
||||
mpz_out_str(stdout, 10, p0);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
mpz_add_ui(aux, aux, 2);
|
||||
}
|
||||
|
||||
printf("The %zu prime is ", prime_list.size);
|
||||
mpz_out_str(stdout, 10, prime_list.last->num);
|
||||
if(test_prime(p1))
|
||||
{
|
||||
mpz_out_str(stdout, 10, p1);
|
||||
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;
|
||||
}
|
||||
|
39
src/prime_test.c
Normal file
39
src/prime_test.c
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
@ -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
|
||||
@ -20,18 +20,8 @@
|
||||
|
||||
#include <gmp.h>
|
||||
|
||||
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);
|
||||
/*
|
||||
* checks to see if the number `p` is prime, returning 1 if it is and 0
|
||||
* if it is not.
|
||||
*/
|
||||
int test_prime(const mpz_t p);
|
Loading…
Reference in New Issue
Block a user