12 Commits
v0.4 ... v0.5

Author SHA1 Message Date
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
7 changed files with 2572 additions and 27 deletions

3
.gitignore vendored
View File

@ -2,5 +2,8 @@
build/*
!build/.keep
# Ignore documentation
doc/
# Ignore vim temporary files
*.sw[a-z]

View File

@ -19,3 +19,7 @@ Change Log
- 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.

2489
Doxyfile Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,6 @@
/**
* This is the number of elements by which the list expands.
* WARNING: Always use doubles for this number (2^X)
*/
#define BLOCK_SIZE 1024
@ -19,7 +18,7 @@ void initList(List *restrict l) {
}
void deInitList(List *restrict l) {
for(ulli i = 0; i < l->size; ++i) {
for(size_t i = 0; i < l->size; ++i) {
mpz_clear(l->list[i]);
}
free(l->list);

View File

@ -1,23 +1,27 @@
/**
* @file list.h
* @author Deathsbreed <deathsbreed@themusicinnoise.net>
* @brief Code responsible for List management.
* @details Code responsible for the definition and management of the
* List object.
*/
#pragma once
#include <gmp.h>
#include <stdbool.h>
#include "types.h"
/**
* @brief An infinitely expanding list type.
*/
typedef struct {
mpz_t *list; //!< The list of elements
ulli size; //!< How many elements are in the list
ulli end; //!< The last element of the list (in use)
size_t size; //!< How many elements are in the list
size_t end; //!< The last element of the list (in use)
} List;
/**
* @brief Initialize a List.
* @details Initialize the list and its variables, allocating memory
* to the pointer array inside. Returns true on success and false on
* failure.
* to the pointer array inside.
* @param[in] l A pointer to a List type to be initialized.
*/
void initList(List *restrict l);

View File

@ -2,21 +2,58 @@
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>
#include <getopt.h>
#include <gmp.h>
#include "list.h"
#include "types.h"
#define VERSION "v0.5"
static bool run;
void printUsage(char *progName);
void leave();
int main(void) {
puts("Indivisible v0.4\n");
int main(int argc, char *argv[]) {
bool f_help = false, f_version = false, f_quiet = false;
int c;
while((c = getopt(argc, argv, "hvq")) != -1) {
switch(c) {
case 'h':
f_help = true;
break;
case 'v':
f_version = true;
break;
case 'q':
f_quiet = true;
break;
default:
printUsage(argv[0]);
exit(1);
}
}
if(f_help) {
printUsage(argv[0]);
puts(" -h print this help information");
puts(" -v print version number of program");
puts(" -q quiet mode");
return 0;
} else if(f_version) {
printf("Indivisible %s\n", VERSION);
return 0;
}
// Quit on ^C by setting `run = false'
run = true;
signal(SIGINT, leave);
if(f_quiet) {
puts("Use Ctrl+C (SIGINT) to exit.");
}
// Primes we've found
List primes;
initList(&primes);
@ -28,11 +65,13 @@ int main(void) {
// Add 2, a known prime to this list
mpz_set_ui(num, 2);
addToList(&primes, num);
if(!f_quiet) {
if(mpz_out_str(stdout, 10, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
exit(1);
}
printf("\n");
}
mpz_add_ui(num, num, 1);
// Variable for half `num'
@ -42,9 +81,11 @@ int main(void) {
do {
// Calculate half of `num'
mpz_fdiv_q_ui(halfNum, num, 2);
// Loop through found primes
for(ulli i = 0; i < primes.end; ++i) {
if(mpz_cmp(primes.list[i], halfNum) >= 0) break;
/**
* Loop through primes we've found until we get to half of the number
* we're analyzing
*/
for(size_t i = 0; mpz_cmp(primes.list[i], halfNum) < 0; ++i) {
// If `num' is divisible by a prime then go to the next number
if(mpz_divisible_p(num, primes.list[i]) != 0)
goto nextPrime;
@ -52,26 +93,33 @@ int main(void) {
// `num' is a prime so we add it to the list and print it
addToList(&primes, num);
if(!f_quiet) {
if(mpz_out_str(stdout, 10, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
exit(1);
}
printf("\n");
}
nextPrime:
// Add 2 (skip even numbers since they're all divisible by 2)
mpz_add_ui(num, num, 2);
} while(run);
printf("Found %zu primes.\n", primes.end);
puts("Clearing memory...");
// Clear GMP variables
mpz_clear(halfNum);
mpz_clear(num);
// Deinitialize the list
deInitList(&primes);
puts("Exit successful.");
return 0;
}
void leave() {
puts("Exiting...\n");
run = false;
void printUsage(char *progName) {
printf("%s [-v | -h | -q]\n", progName);
}
void leave() { run = false; }

View File

@ -1,2 +0,0 @@
#pragma once
typedef unsigned long long int ulli;