121 Commits
v0.1 ... master

Author SHA1 Message Date
23b8d988e8 Added useful comments. 2019-10-08 18:02:45 +02:00
84687f86c1 Using a better algorithm based on property of primes.
I recently found out that `p` being prime, while `p > 3`, then
`p = 6*k (+/-) 1`.
2019-10-08 17:58:08 +02:00
2cfc24afb7 Forgot to print 2. 2019-10-08 17:10:39 +02:00
d61ef9b034 Entire OpenMPI library is required. 2019-10-08 15:36:33 +02:00
23592a98ed Create linked list interface. 2019-09-24 08:20:09 +02:00
fadcafaeaf Begin linking to MPI. 2019-09-24 08:19:47 +02:00
63e2eda663 Switch to using linked list. 2019-09-24 07:10:12 +02:00
d5fb2b06df New objective for v2.0 2019-09-24 07:07:26 +02:00
68ac85f3f0 Code refractoring. 2019-09-23 20:44:23 +02:00
4d470a14de Use the MPI compiler. 2018-10-21 10:24:57 +02:00
874cc75431 Only test up to the sqrt. 2018-10-19 09:05:46 +02:00
d2e037e357 Added MPI flags. 2018-10-18 15:26:02 +02:00
90158fb04c Doxygen is no longer in use. 2018-10-18 15:08:29 +02:00
bb26ae9b9f Removed CI file. 2018-10-18 15:07:47 +02:00
1b0b8fd443 Removed args.h file. 2018-10-18 15:05:33 +02:00
e06e40af3b Implement verbose option. 2018-10-18 15:05:01 +02:00
eaa238a900 Basic prime calculator. 2018-10-18 14:56:15 +02:00
bd3181a1a0 Added linked list. 2018-10-18 12:50:57 +02:00
89811c1d02 Added new source files. 2018-10-18 12:04:07 +02:00
01a32c0e52 Fresh new start. 2018-10-18 12:00:32 +02:00
7aceb7fcc7 Remove CMake policy.
The old policy caused issues because it's about to be lost in the newer
versions of CMake, so we'll have to deal with warnings from CMake until
the FindOpenMP.cmake file is updated.
2017-10-03 16:53:34 +02:00
3348d52c49 Don't allow writing to the same file. 2017-09-17 05:46:03 +02:00
c73bfd1501 Changing BLOCK_SIZE default and allowing override.
The larger BLOCK_SIZE is the less time it has to spend expanding memory,
the faster it gets.
2017-08-03 12:57:34 -05:00
2afe144e7f Give install instructions. 2017-03-27 16:22:16 +02:00
a988711918 Fixed progress measurement. 2017-03-16 14:59:53 +01:00
74a82e2038 Case insensitive. 2017-03-16 14:59:26 +01:00
659bdeee27 Handle export errors. 2017-02-14 12:57:11 +01:00
d39a51baa2 Add license information to all source files. 2017-02-14 12:49:54 +01:00
7fa4d9d2bf Added stuff to CHANGELOG for v1.0 2017-02-13 17:08:30 +01:00
6ed153b9e2 Will be disabling merge requests. 2017-02-13 10:09:25 +01:00
c8d1c095ba Clarify. 2017-02-13 09:56:23 +01:00
ae8cbed179 Details... 2017-02-13 09:55:31 +01:00
69028a15c8 Promote patches. 2017-02-13 09:54:13 +01:00
b20733f3ac Append new primes to the end of a file. 2017-02-13 09:47:47 +01:00
2b5541d2c3 Fixed some memory leaks. 2017-02-12 19:33:57 +01:00
94248612ba Find the nth prime. 2017-02-06 18:00:34 +01:00
8ee18c8c4b Forget it, it's unnecessary. 2017-02-06 17:51:07 +01:00
bdea1979e8 Use inline functions. 2017-02-06 17:50:26 +01:00
a3ec19d47c Cleaned the code. 2017-02-02 19:53:29 +01:00
22239ec644 Preparing for v1.0 release. 2017-02-02 10:46:34 +01:00
22278999e7 Fixed algorithm problem. 2017-02-02 10:35:43 +01:00
a5b68282b3 Cancel if `primes.list[i] >= numRoot'.
It has been solved!!!
2017-02-01 23:12:23 +01:00
b9cafadf8e Parallelized, but needs algorithm optimization. 2017-02-01 19:36:05 +01:00
32076a67cd Remove parallelization code to start anew. 2017-02-01 17:18:03 +01:00
9673a4da57 Use shared as default. 2017-01-31 22:24:06 +01:00
76936644cd Almost completely parallelized. 2017-01-31 19:39:35 +01:00
a6ecea514f Restrict will no longer apply to `addToList' 2017-01-17 00:11:51 +01:00
22c7702cf0 We only need to test up to the sqrt(num). 2017-01-01 17:05:25 +01:00
f8726497a4 Don't test for divisibility by 2.
We skip all even numbers, so we can skip 2, this also means giving 3 as
a given.
2016-12-30 17:57:28 +01:00
3e902efd18 Small cleanup. 2016-12-30 17:51:48 +01:00
3246870b75 Better help information. 2016-12-28 14:43:17 +01:00
09166efe89 Small improvements and optimizations.
All error catching is now in `main.c' so we can neatly release memory as
well as adding the `restrict' optimizer to the `addToList()' function.
2016-12-28 00:30:31 +01:00
8b35c5aea3 Fixed title for v0.7 2016-12-27 22:53:22 +01:00
1264edc8c8 We're going to release v0.7 first. 2016-12-27 22:52:06 +01:00
63aa8e14cb Fixed the damn exporting for once.
Now I'm checking to see if it was actually worth a damn to have raw
files in the first place (-_-;).
2016-12-27 22:34:34 +01:00
f2eb3e869e Fixed the file issue...
but the export issue still remains -_- I hate seg faults.
2016-12-27 21:47:16 +01:00
a695cce709 Moving shit to files.c 2016-12-27 21:31:24 +01:00
096cb2eb16 Moving file I/O to own file. Everything works except export. 2016-12-26 18:50:51 +01:00
1783b16024 Use raw I/O for file saving. 2016-12-26 16:51:51 +01:00
c602b5fe1b Added OpenMP to CI. 2016-12-26 16:35:52 +01:00
6b2411e860 Now using OpenMP for parallel programming and next version is v1.0 2016-12-26 15:57:28 +01:00
6c2f96416b Read and write primes to and from file. 2016-12-26 15:56:56 +01:00
fa3f2dd2b1 New goal before paralellization 2016-12-22 16:31:24 +01:00
591ee92971 Cast is unnecessary in C. 2016-12-15 15:16:04 +01:00
803c6f9e06 `size_t' makes this safeguard unnecessary. 2016-12-14 23:42:20 +01:00
4390fca3ef Added comments. 2016-12-14 23:38:22 +01:00
2629c12f1a Set goal for v1.0 2016-12-14 22:26:43 +01:00
af79d206d3 Added entry for v0.6 2016-12-14 20:12:46 +01:00
4b034ce5e3 Print more info about the file writing. 2016-12-14 19:34:50 +01:00
f146dbf11c Able to save found primes to a file afterwards. 2016-12-14 19:28:28 +01:00
656fee720e Allow user to choose base. 2016-12-14 19:06:33 +01:00
5e45656e1a Forgot that from the switch to C. 2016-12-14 17:58:12 +01:00
c522196d66 Argument parsing! 2016-12-14 17:52:52 +01:00
b5dcadce19 Added new goal for v0.5 2016-12-14 17:31:36 +01:00
003b94dcdb Added some more useful docs and stuff. 2016-12-14 16:14:58 +01:00
fcee95da17 Fixed something from the description 2016-12-14 16:14:37 +01:00
43620ba2d3 Enable documentation. 2016-12-14 16:08:11 +01:00
9f1160242a Removed warning (no longer necessary). 2016-12-14 15:56:28 +01:00
b414bff9dc Minor optimization. 2016-12-14 15:55:39 +01:00
d8c81b172b Print some fun information at the end. 2016-12-14 15:29:06 +01:00
c2f0fb0ffd Defining changes for v0.5 2016-12-14 15:05:24 +01:00
5bbac132bc Use size_t, which is better for arrays of very large sizes. 2016-12-14 14:13:32 +01:00
2a3e97f4bc Prepare version number so I don't forget later. 2016-12-13 18:09:05 +01:00
06d5ddb0cc Fixed problem with the algorithm.
If it's equal to half then we want to check if it's divisible, since if
it's half then it is NOT prime.
2016-12-13 18:05:02 +01:00
a9019291c2 New goal for v0.4 2016-12-13 17:51:48 +01:00
5aa0b333c0 Made a fix, because previously it was not measuring primes. 2016-12-13 16:32:10 +01:00
3110c74174 Added entry about `restrict' keyword. 2016-12-13 16:12:31 +01:00
a5ce845c68 `restrict' keyword must be in declaration. 2016-12-13 16:12:03 +01:00
449fef2994 Add restrict
Add restrict for better pointer optimizations. This is not being applied
to `addToList()' because that function we want to thread later on.
2016-12-13 11:26:12 +01:00
2e9326b5fb Preparing for v0.4
I'm going to be learning OpenCL, after which I will decide whether to
use OpenCL or OpenMP for the development of Indivisible. This mostly
depends on the compatibility of each library with GMP, especially since
I believe GMP already does some of its own threading.
2016-12-12 23:21:22 +01:00
06cb271dba Forgot to set that shit to v0.3 2016-12-12 16:20:21 +01:00
4cbc3fae7d Cache the right .o files. 2016-12-10 17:43:31 +01:00
9b4fa96474 Remove TODO list. 2016-12-10 17:41:40 +01:00
e2aedab3b0 Added new entry. 2016-12-10 17:12:58 +01:00
bc8b48dd29 Updated info for v0.3 2016-12-10 17:12:00 +01:00
dab78093ab Deleted macros 2016-12-10 17:11:21 +01:00
a782bdb57d Organized TODO.md 2016-12-10 15:31:04 +01:00
9157d15383 Added goals for v0.3 2016-12-10 15:30:51 +01:00
f1fd758bfc Added entry for v0.2.1 (which I had previously forgotten). 2016-12-10 14:54:12 +01:00
66c0a5d027 Added a TODO list. 2016-12-10 14:54:00 +01:00
f4ee9872bc Optimize the algorithm to avoid numbers larger than half. 2016-12-10 14:46:51 +01:00
8a42e85d04 Better explanation of error. 2016-12-10 11:51:32 +01:00
dd38b53e31 Fixed leaks. 2016-12-10 11:20:01 +01:00
3c8b9922fb Shorten the long ass `unsigned long long int' to ulli. 2016-12-10 02:20:57 +01:00
cb9e1648e9 No need to use that macro outside a loop. 2016-12-10 02:09:34 +01:00
30703314dd Forgot it for the main loop as well. 2016-12-10 01:15:52 +01:00
4905391c82 Add more optimizations. 2016-12-10 00:50:23 +01:00
d757f3a79f Added section for v0.2 2016-12-09 23:26:27 +01:00
79a9ba11ff Switch to C, stop using OpenMP, start using GMP. 2016-12-09 23:02:51 +01:00
05c50652b8 Added GMP as a deps for CI. 2016-12-02 16:35:07 +01:00
7d6ed15523 We only have to define the universal CXX flags once. 2016-12-02 16:34:12 +01:00
b66755174f GMP is a new dependency. 2016-12-02 16:31:12 +01:00
04a8d7ddde Downgrade to C++11 to support g++4.8 2016-12-02 15:55:29 +01:00
6bfab05e37 Use lambda function instead. 2016-12-01 12:35:15 +01:00
05b6257e44 Skip all even numbers since they're all divisible by 2. 2016-12-01 12:21:09 +01:00
1593e57e90 CMake check for OpenMP 2016-11-30 22:20:46 +01:00
75b35b3356 Fixed seg fault (I think). 2016-11-30 21:37:10 +01:00
3bb757dc0d Basic multi-core capabilities.
It's buggy, at some point it gives a segmentation fault at around the
`for(auto i : primes)` part. I think I should add a pragma critical
there.
2016-11-30 21:08:44 +01:00
1032e099a1 Added Vim swap files to gitignore. 2016-11-30 20:34:06 +01:00
a236f88d12 Added libgomp to dependencies. 2016-11-30 19:10:00 +01:00
87b96e4ebc Added OpenMP to the project. 2016-11-30 19:09:29 +01:00
19 changed files with 411 additions and 143 deletions

9
.gitignore vendored
View File

@ -1,3 +1,6 @@
# Ignore build files # Ignore binary files
build/* *.o
!build/.keep indivisible
# Ignore vim temporary files
*.sw[a-z]

View File

@ -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

View File

@ -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 |

View File

@ -1,7 +1,42 @@
Change Log Change Log
========== ==========
- v0.1: Initial release - v0.1: Initial release
- Basic prime calculation. - Basic prime calculation.
- Uses a growing vector of known primes and gets the modulus of the number divided by these primes. - 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.

View File

@ -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})

View File

@ -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.

View File

@ -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:
{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 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 Normal file
View 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
View 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).

View File

@ -1,25 +0,0 @@
Indivisible
===========
[![build status](https://gitlab.com/Deathsbreed/Indivisible/badges/master/build.svg)](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.

View File

@ -1,3 +0,0 @@
--lineend=linux
--indent=tab
--style=attach

View File

View File

@ -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
View 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
View 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
View 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
View 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
View 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
View 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);