Compare commits
121 Commits
Author | SHA1 | Date | |
---|---|---|---|
23b8d988e8 | |||
84687f86c1 | |||
2cfc24afb7 | |||
d61ef9b034 | |||
23592a98ed | |||
fadcafaeaf | |||
63e2eda663 | |||
d5fb2b06df | |||
68ac85f3f0 | |||
4d470a14de | |||
874cc75431 | |||
d2e037e357 | |||
90158fb04c | |||
bb26ae9b9f | |||
1b0b8fd443 | |||
e06e40af3b | |||
eaa238a900 | |||
bd3181a1a0 | |||
89811c1d02 | |||
01a32c0e52 | |||
7aceb7fcc7 | |||
3348d52c49 | |||
c73bfd1501 | |||
2afe144e7f | |||
a988711918 | |||
74a82e2038 | |||
659bdeee27 | |||
d39a51baa2 | |||
7fa4d9d2bf | |||
6ed153b9e2 | |||
c8d1c095ba | |||
ae8cbed179 | |||
69028a15c8 | |||
b20733f3ac | |||
2b5541d2c3 | |||
94248612ba | |||
8ee18c8c4b | |||
bdea1979e8 | |||
a3ec19d47c | |||
22239ec644 | |||
22278999e7 | |||
a5b68282b3 | |||
b9cafadf8e | |||
32076a67cd | |||
9673a4da57 | |||
76936644cd | |||
a6ecea514f | |||
22c7702cf0 | |||
f8726497a4 | |||
3e902efd18 | |||
3246870b75 | |||
09166efe89 | |||
8b35c5aea3 | |||
1264edc8c8 | |||
63aa8e14cb | |||
f2eb3e869e | |||
a695cce709 | |||
096cb2eb16 | |||
1783b16024 | |||
c602b5fe1b | |||
6b2411e860 | |||
6c2f96416b | |||
fa3f2dd2b1 | |||
591ee92971 | |||
803c6f9e06 | |||
4390fca3ef | |||
2629c12f1a | |||
af79d206d3 | |||
4b034ce5e3 | |||
f146dbf11c | |||
656fee720e | |||
5e45656e1a | |||
c522196d66 | |||
b5dcadce19 | |||
003b94dcdb | |||
fcee95da17 | |||
43620ba2d3 | |||
9f1160242a | |||
b414bff9dc | |||
d8c81b172b | |||
c2f0fb0ffd | |||
5bbac132bc | |||
2a3e97f4bc | |||
06d5ddb0cc | |||
a9019291c2 | |||
5aa0b333c0 | |||
3110c74174 | |||
a5ce845c68 | |||
449fef2994 | |||
2e9326b5fb | |||
06cb271dba | |||
4cbc3fae7d | |||
9b4fa96474 | |||
e2aedab3b0 | |||
bc8b48dd29 | |||
dab78093ab | |||
a782bdb57d | |||
9157d15383 | |||
f1fd758bfc | |||
66c0a5d027 | |||
f4ee9872bc | |||
8a42e85d04 | |||
dd38b53e31 | |||
3c8b9922fb | |||
cb9e1648e9 | |||
30703314dd | |||
4905391c82 | |||
d757f3a79f | |||
79a9ba11ff | |||
05c50652b8 | |||
7d6ed15523 | |||
b66755174f | |||
04a8d7ddde | |||
6bfab05e37 | |||
05b6257e44 | |||
1593e57e90 | |||
75b35b3356 | |||
3bb757dc0d | |||
1032e099a1 | |||
a236f88d12 | |||
87b96e4ebc |
9
.gitignore
vendored
9
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
# Ignore build files
|
||||
build/*
|
||||
!build/.keep
|
||||
# Ignore binary files
|
||||
*.o
|
||||
indivisible
|
||||
|
||||
# Ignore vim temporary files
|
||||
*.sw[a-z]
|
||||
|
@ -1,13 +0,0 @@
|
||||
image: gcc
|
||||
|
||||
build:
|
||||
stage: build
|
||||
before_script:
|
||||
- apt update && apt -y install cmake
|
||||
script:
|
||||
- cd build/
|
||||
- cmake -DCMAKE_BUILD_TYPE=Release ..
|
||||
- make
|
||||
artifacts:
|
||||
paths:
|
||||
- build/indivisible
|
@ -1,8 +0,0 @@
|
||||
Authors
|
||||
=======
|
||||
These are a list of people that have contributed to the Uhn project.
|
||||
|
||||
| **Name/Alias** | **E-Mail** |
|
||||
|---------------------------|---------------------------------------|
|
||||
| Deathsbreed | deathsbreed@themusicinnoise.net |
|
||||
|
39
CHANGELOG
39
CHANGELOG
@ -1,7 +1,42 @@
|
||||
Change Log
|
||||
==========
|
||||
|
||||
- v0.1: Initial release
|
||||
- v0.1: Initial release
|
||||
- Basic prime calculation.
|
||||
- Uses a growing vector of known primes and gets the modulus of the number divided by these primes.
|
||||
- Uses type `long long` to hold prime numbers.
|
||||
- Uses type `long long' to hold prime numbers.
|
||||
- v0.2: Multi-Precision
|
||||
- Switch to C.
|
||||
- Uses GNU Multiple Precision library (GMP) to hold prime numbers, allowing for 'infinite' size.
|
||||
- Add `likely()' and `unlikely()' macros to optimize.
|
||||
- v0.2.1: Memory Leak Fixes
|
||||
- Fixed a major memory leak at the end of the program.
|
||||
- Added more optimizers.
|
||||
- v0.3: Optimizations
|
||||
- Algorithm skips half the known primes.
|
||||
- Removed `likely()' and `unlikely()' macros due to lack of information.
|
||||
- Improved performance.
|
||||
- v0.4: Fixed Algorithm
|
||||
- Fixed algorithm to actually calculate primes.
|
||||
- Added extra C99 optimizations.
|
||||
- v0.5: Minor Changes
|
||||
- Use `size_t' instead of `unsigned long long int'.
|
||||
- Minor optimizations to the algorithm.
|
||||
- Added commandline argument parsing.
|
||||
- v0.6: User Control
|
||||
- Allow user to choose base in which the prime numbers are printed.
|
||||
- Give option for primes to be saved to a file upon exit.
|
||||
- Free memory and leave instead of emergency exit.
|
||||
- v0.7: Data Saving
|
||||
- Allow user to save found primes to be loaded later.
|
||||
- User can save and read primes in raw output.
|
||||
- v1.0: Parallelization
|
||||
- Now completely parallelized.
|
||||
- Cleaned up/organized the code-base
|
||||
- Exporting files uses less memory
|
||||
- Allow searching for the nth prime number
|
||||
- Clearing some memory leaks
|
||||
- v2.0: Linked List
|
||||
- Code cleanup.
|
||||
- Replace OpenMP code with OpenMPI.
|
||||
- Use linked list for prime storage.
|
||||
|
@ -1,23 +0,0 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
project(Indivisible)
|
||||
|
||||
set(TARGET_NAME indivisible)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
endif()
|
||||
|
||||
set(SRCS
|
||||
src/Main.cpp)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-std=c++14 -fno-elide-constructors -pedantic-errors -Wall -Wextra -Werror -Wpedantic -Winit-self -Wmissing-declarations -Wuninitialized -Wfatal-errors")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O0")
|
||||
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} -g -O3")
|
||||
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} -Os")
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE MATCHES Debug AND NOT CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
|
||||
add_definitions(-DNDEBUG)
|
||||
endif()
|
||||
|
||||
add_executable(${TARGET_NAME} ${SRCS})
|
@ -1,23 +0,0 @@
|
||||
Contributing
|
||||
============
|
||||
We gladly accept contributions to Indivisible, however there are a few guidelines that must be followed.
|
||||
|
||||
1) Copyright & Licensing
|
||||
------------------------
|
||||
The copyright of this project and all of its code, unless stated otherwise, belongs to Nicolás Ortega and is licensed with the [GNU GPLv3](/LICENSE). This also applies to all contributions (contributors may add themselves to the [authors file](/AUTHORS.md) for recognition). This is purely for legal purposes, there is no intention to deny your contributions.
|
||||
|
||||
2) Documentation
|
||||
----------------
|
||||
All new features added must be well documented using DOxygen style comments. If your merge request adds a new feature and it is not well documented we will ask you to finish documenting the code before accepting a merge. This is simply so that both the maintainers of this repo and other users can understand the new API that bas been added.
|
||||
|
||||
3) Code-Style
|
||||
-------------
|
||||
The only aspects that will be religiously upheld for all contributions to this repo are indentation and bracket placement. All indentation must be tab characters (reason being so that everyone can view the code with the indentation that they prefer rather than being forced to see a specific indentation), and brackets must be attached. Everything else is minor and can be overlooked.
|
||||
|
||||
Things that would be nice is if you could avoid having the lines being too long, and please avoid having more than one empty line in a row.
|
||||
|
||||
4) Fractured Merge Requests
|
||||
---------------------------
|
||||
Please make sure your merge requests are fractured into separate parts so that it may be easy for us to deal with them individually. The reason for this is to avoid having merge requests that both fix bugs and add features when in reality they should be separate requests. The only exception to this will be if the changes made to the code depend on one another to function properly.
|
||||
|
||||
If your merge request meets all these requirements and is passed by one of the moderators it will be merged into the main repo.
|
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/
|
34
README
Normal file
34
README
Normal file
@ -0,0 +1,34 @@
|
||||
===================
|
||||
*** Indivisible ***
|
||||
===================
|
||||
A parallelized prime number generator written in C. It uses a bignum
|
||||
library as well as OpenMPI for parallelization.
|
||||
|
||||
# Building
|
||||
----------
|
||||
## 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
|
||||
|
||||
## 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 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 terms & conditions of the GNU General
|
||||
Public License version 3 or greater (see `LICENSE` file).
|
25
README.md
25
README.md
@ -1,25 +0,0 @@
|
||||
Indivisible
|
||||
===========
|
||||
[](https://gitlab.com/Deathsbreed/Indivisible/commits/master)
|
||||
|
||||
Indivisible is an optimized prime number generator written in C++.
|
||||
|
||||
Building
|
||||
--------
|
||||
This project uses CMake to build.
|
||||
|
||||
```bash
|
||||
$ cd build/
|
||||
$ cmake ..
|
||||
$ make
|
||||
```
|
||||
|
||||
To build a release build run `cmake -DCMAKE_BUILD_TYPE=Release ..` instead.
|
||||
|
||||
Contributing
|
||||
------------
|
||||
If you would like to contribute to Uhn then please read the [contributing file](/CONTRIBUTING.md) before creating a merge request.
|
||||
|
||||
License
|
||||
-------
|
||||
This project is licensed with the [GNU GPLv3](/LICENSE). Since it is an educational project I find it very important that all contributions continue to remain free/libre.
|
@ -1,3 +0,0 @@
|
||||
--lineend=linux
|
||||
--indent=tab
|
||||
--style=attach
|
38
src/Main.cpp
38
src/Main.cpp
@ -1,38 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <csignal>
|
||||
|
||||
static bool run;
|
||||
|
||||
void leave(int signum);
|
||||
|
||||
int main(void) {
|
||||
std::cout << "Indivisible v0.1\n";
|
||||
run = true;
|
||||
|
||||
signal(SIGINT, leave);
|
||||
|
||||
std::vector<unsigned long long> primes;
|
||||
primes.push_back(2);
|
||||
unsigned long long num = 2;
|
||||
|
||||
while(run) {
|
||||
bool isPrime = true;
|
||||
for(auto i : primes) {
|
||||
if(i > num / 2) break;
|
||||
if(num % i == 0) isPrime = false;
|
||||
}
|
||||
if(isPrime) {
|
||||
primes.push_back(num);
|
||||
std::cout << num << std::endl;
|
||||
}
|
||||
++num;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void leave(int signum) {
|
||||
std::cout << "Exiting (" << signum << ")\n";
|
||||
run = false;
|
||||
}
|
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
|
61
src/llist.c
Normal file
61
src/llist.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 "llist.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void llist_init(struct llist *list) {
|
||||
list->first = NULL;
|
||||
list->last = NULL;
|
||||
list->size = 0;
|
||||
}
|
||||
|
||||
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(!aux)
|
||||
{
|
||||
fprintf(stderr, "ERROR llist_insert(): failed to allocate memory");
|
||||
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)
|
||||
list->first = aux;
|
||||
list->last = aux;
|
||||
list->size++;
|
||||
|
||||
return 1;
|
||||
}
|
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);
|
70
src/main.c
Normal file
70
src/main.c
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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 "globals.h"
|
||||
#include "llist.h"
|
||||
#include "prime_test.h"
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <gmp.h>
|
||||
|
||||
int run = 1;
|
||||
|
||||
void quit_signal(int signum) {
|
||||
printf("Received signal %d. Ending process...\n", signum);
|
||||
run = 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%s v%s\n", APP_NAME, VERSION);
|
||||
|
||||
signal(SIGINT, quit_signal);
|
||||
|
||||
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
|
||||
|
||||
puts("2");
|
||||
puts("3");
|
||||
|
||||
while(run)
|
||||
{
|
||||
mpz_sub_ui(p0, test_base, 1); // p0 = test_base - 1
|
||||
mpz_add_ui(p1, test_base, 1); // p0 = test_base + 1
|
||||
|
||||
if(test_prime(p0))
|
||||
{
|
||||
mpz_out_str(stdout, 10, p0);
|
||||
printf("\n");
|
||||
}
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
27
src/prime_test.h
Normal file
27
src/prime_test.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
|
||||
|
||||
#include <gmp.h>
|
||||
|
||||
/*
|
||||
* 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);
|
Reference in New Issue
Block a user