Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
af79d206d3 | |||
4b034ce5e3 | |||
f146dbf11c | |||
656fee720e | |||
5e45656e1a | |||
c522196d66 | |||
b5dcadce19 | |||
003b94dcdb | |||
fcee95da17 | |||
43620ba2d3 | |||
9f1160242a | |||
b414bff9dc | |||
d8c81b172b | |||
c2f0fb0ffd | |||
5bbac132bc | |||
2a3e97f4bc | |||
06d5ddb0cc |
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,5 +2,8 @@
|
|||||||
build/*
|
build/*
|
||||||
!build/.keep
|
!build/.keep
|
||||||
|
|
||||||
|
# Ignore documentation
|
||||||
|
doc/
|
||||||
|
|
||||||
# Ignore vim temporary files
|
# Ignore vim temporary files
|
||||||
*.sw[a-z]
|
*.sw[a-z]
|
||||||
|
@ -19,3 +19,11 @@ Change Log
|
|||||||
- v0.4: Fixed Algorithm
|
- v0.4: Fixed Algorithm
|
||||||
- Fixed algorithm to actually calculate primes.
|
- Fixed algorithm to actually calculate primes.
|
||||||
- Added extra C99 optimizations.
|
- 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.
|
||||||
|
@ -24,10 +24,10 @@ set(SRCS
|
|||||||
|
|
||||||
# Define the C flags.
|
# Define the C flags.
|
||||||
set(CMAKE_C_FLAGS "-std=gnu99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
|
set(CMAKE_C_FLAGS "-std=gnu99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
|
||||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g -O0")
|
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
|
||||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3")
|
set(CMAKE_C_FLAGS_RELEASE "-O3")
|
||||||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS} -g -O3")
|
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3")
|
||||||
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS} -Os")
|
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
|
||||||
|
|
||||||
if(NOT CMAKE_BUILD_TYPE MATCHES Debug AND NOT CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
|
if(NOT CMAKE_BUILD_TYPE MATCHES Debug AND NOT CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
|
||||||
add_definitions(-DNDEBUG)
|
add_definitions(-DNDEBUG)
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the number of elements by which the list expands.
|
* This is the number of elements by which the list expands.
|
||||||
* WARNING: Always use doubles for this number (2^X)
|
|
||||||
*/
|
*/
|
||||||
#define BLOCK_SIZE 1024
|
#define BLOCK_SIZE 1024
|
||||||
|
|
||||||
@ -19,7 +18,7 @@ void initList(List *restrict l) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void deInitList(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]);
|
mpz_clear(l->list[i]);
|
||||||
}
|
}
|
||||||
free(l->list);
|
free(l->list);
|
||||||
|
16
src/list.h
16
src/list.h
@ -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
|
#pragma once
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief An infinitely expanding list type.
|
* @brief An infinitely expanding list type.
|
||||||
*/
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
mpz_t *list; //!< The list of elements
|
mpz_t *list; //!< The list of elements
|
||||||
ulli size; //!< How many elements are in the list
|
size_t size; //!< How many elements are in the list
|
||||||
ulli end; //!< The last element of the list (in use)
|
size_t end; //!< The last element of the list (in use)
|
||||||
} List;
|
} List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize a List.
|
* @brief Initialize a List.
|
||||||
* @details Initialize the list and its variables, allocating memory
|
* @details Initialize the list and its variables, allocating memory
|
||||||
* to the pointer array inside. Returns true on success and false on
|
* to the pointer array inside.
|
||||||
* failure.
|
|
||||||
* @param[in] l A pointer to a List type to be initialized.
|
* @param[in] l A pointer to a List type to be initialized.
|
||||||
*/
|
*/
|
||||||
void initList(List *restrict l);
|
void initList(List *restrict l);
|
||||||
|
128
src/main.c
128
src/main.c
@ -2,21 +2,76 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "types.h"
|
|
||||||
|
#define VERSION "v0.6"
|
||||||
|
|
||||||
static bool run;
|
static bool run;
|
||||||
|
|
||||||
|
void printUsage(char *progName);
|
||||||
void leave();
|
void leave();
|
||||||
|
|
||||||
int main(void) {
|
int main(int argc, char *argv[]) {
|
||||||
puts("Indivisible v0.4\n");
|
bool f_help = false,
|
||||||
|
f_version = false,
|
||||||
|
f_quiet = false;
|
||||||
|
int base = 10;
|
||||||
|
char *file = NULL;
|
||||||
|
|
||||||
|
int c;
|
||||||
|
while((c = getopt(argc, argv, "hvqb:f:")) != -1) {
|
||||||
|
switch(c) {
|
||||||
|
case 'h':
|
||||||
|
f_help = true;
|
||||||
|
break;
|
||||||
|
case 'v':
|
||||||
|
f_version = true;
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
f_quiet = true;
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
base = atoi(optarg);
|
||||||
|
if(base < 2 || base > 62) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Invalid base `%d'. Base must be between 2 and 62\n",
|
||||||
|
base);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
file = optarg;
|
||||||
|
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");
|
||||||
|
puts(" -b <base> base in which to print primes between 2 and 62");
|
||||||
|
puts(" -f <file> file to save primes to");
|
||||||
|
return 0;
|
||||||
|
} else if(f_version) {
|
||||||
|
printf("Indivisible %s\n", VERSION);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Quit on ^C by setting `run = false'
|
// Quit on ^C by setting `run = false'
|
||||||
run = true;
|
run = true;
|
||||||
signal(SIGINT, leave);
|
signal(SIGINT, leave);
|
||||||
|
|
||||||
|
if(f_quiet) {
|
||||||
|
puts("Use Ctrl+C (SIGINT) to exit.");
|
||||||
|
}
|
||||||
|
|
||||||
// Primes we've found
|
// Primes we've found
|
||||||
List primes;
|
List primes;
|
||||||
initList(&primes);
|
initList(&primes);
|
||||||
@ -28,11 +83,13 @@ int main(void) {
|
|||||||
// Add 2, a known prime to this list
|
// Add 2, a known prime to this list
|
||||||
mpz_set_ui(num, 2);
|
mpz_set_ui(num, 2);
|
||||||
addToList(&primes, num);
|
addToList(&primes, num);
|
||||||
if(mpz_out_str(stdout, 10, num) == 0) {
|
if(!f_quiet) {
|
||||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
if(mpz_out_str(stdout, base, num) == 0) {
|
||||||
exit(1);
|
fprintf(stderr, "Could not print to `stdout'!\n");
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("\n");
|
|
||||||
mpz_add_ui(num, num, 1);
|
mpz_add_ui(num, num, 1);
|
||||||
|
|
||||||
// Variable for half `num'
|
// Variable for half `num'
|
||||||
@ -42,9 +99,11 @@ int main(void) {
|
|||||||
do {
|
do {
|
||||||
// Calculate half of `num'
|
// Calculate half of `num'
|
||||||
mpz_fdiv_q_ui(halfNum, num, 2);
|
mpz_fdiv_q_ui(halfNum, num, 2);
|
||||||
// Loop through found primes
|
/**
|
||||||
for(ulli i = 0; i < primes.end; ++i) {
|
* Loop through primes we've found until we get to half of the number
|
||||||
if(mpz_cmp(primes.list[i], halfNum) >= 0) break;
|
* 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 `num' is divisible by a prime then go to the next number
|
||||||
if(mpz_divisible_p(num, primes.list[i]) != 0)
|
if(mpz_divisible_p(num, primes.list[i]) != 0)
|
||||||
goto nextPrime;
|
goto nextPrime;
|
||||||
@ -52,26 +111,61 @@ int main(void) {
|
|||||||
|
|
||||||
// `num' is a prime so we add it to the list and print it
|
// `num' is a prime so we add it to the list and print it
|
||||||
addToList(&primes, num);
|
addToList(&primes, num);
|
||||||
if(mpz_out_str(stdout, 10, num) == 0) {
|
if(!f_quiet) {
|
||||||
fprintf(stderr, "Could not print to `stdout'!\n");
|
if(mpz_out_str(stdout, base, num) == 0) {
|
||||||
exit(1);
|
fprintf(stderr, "Could not print to `stdout'!\n");
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
nextPrime:
|
nextPrime:
|
||||||
// Add 2 (skip even numbers since they're all divisible by 2)
|
// Add 2 (skip even numbers since they're all divisible by 2)
|
||||||
mpz_add_ui(num, num, 2);
|
mpz_add_ui(num, num, 2);
|
||||||
} while(run);
|
} while(run);
|
||||||
|
|
||||||
|
printf("Found %zu primes.\n", primes.end);
|
||||||
// Clear GMP variables
|
// Clear GMP variables
|
||||||
mpz_clear(halfNum);
|
mpz_clear(halfNum);
|
||||||
mpz_clear(num);
|
mpz_clear(num);
|
||||||
|
|
||||||
|
if(file != NULL) {
|
||||||
|
FILE *outFile = fopen(file, "w+");
|
||||||
|
if(outFile == NULL) {
|
||||||
|
fprintf(stderr, "Failed create file `%s'.\n", file);
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
|
printf("Writing primes to `%s'...\n", file);
|
||||||
|
puts("0%");
|
||||||
|
for(size_t i = 0; i < primes.end; ++i) {
|
||||||
|
if(mpz_out_str(outFile, base, primes.list[i]) == 0) {
|
||||||
|
fprintf(stderr, "Error occurred while writing to file `%s'.\n", file);
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
|
fprintf(outFile, "\n");
|
||||||
|
if(i == primes.end / 4) puts("25%");
|
||||||
|
else if(i == primes.end / 2) puts("50%");
|
||||||
|
else if(i == primes.end * 3 / 4) puts("75%");
|
||||||
|
}
|
||||||
|
if(fclose(outFile) != 0) {
|
||||||
|
fprintf(stderr, "Failed to close file `%s'.\n", file);
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
|
puts("100%");
|
||||||
|
puts("Finished writing primes.");
|
||||||
|
}
|
||||||
|
|
||||||
|
releaseMemory:
|
||||||
|
puts("Clearing memory...");
|
||||||
// Deinitialize the list
|
// Deinitialize the list
|
||||||
deInitList(&primes);
|
deInitList(&primes);
|
||||||
|
|
||||||
|
puts("Exit successful.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void leave() {
|
void printUsage(char *progName) {
|
||||||
puts("Exiting...\n");
|
printf("%s [options...]\n", progName);
|
||||||
run = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void leave() { run = false; }
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
typedef unsigned long long int ulli;
|
|
Reference in New Issue
Block a user