From 152d9c60ef038e382c3f35e8299555b7bb4f3f93 Mon Sep 17 00:00:00 2001 From: Hector Colon Date: Wed, 6 Oct 2021 20:42:54 -0400 Subject: [PATCH] add makefile and clean git repo --- .gitignore | 2 + Makefile | 20 +++++++++ mper_gmp.c | 122 --------------------------------------------------- proth.c | 14 +++--- proth_test.c | 35 --------------- 5 files changed, 31 insertions(+), 162 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile delete mode 100755 mper_gmp.c delete mode 100755 proth_test.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..748e98e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +proth +todo diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..344d71a --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +CC = gcc +COMPILER_FLAGS = -Wall + +CSRC = *.c + +ifeq ($(debug),1) + COMPILER_FLAGS += -g +else + COMPILER_FLAGS += -Werror -O2 +endif + +#LINKER_FLAGS specifies the libraries we're linking against +LINKER_FLAGS = -lgmp -lpthread + +#OBJ_NAME specifies the name of our exectuable +BIN_NAME = proth + +#This is the target that compiles our executable +all : $(CSRC) + $(CC) $(CSRC) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(BIN_NAME) diff --git a/mper_gmp.c b/mper_gmp.c deleted file mode 100755 index 042fccc..0000000 --- a/mper_gmp.c +++ /dev/null @@ -1,122 +0,0 @@ -#include -#include -#include -#include -#include - -#include - -#define NUM_DIGITS 1024 - -void mpz_mper(mpz_t num); - -bool inc_mper(char *num); - -char tmp_buf[NUM_DIGITS] = ""; - -uint64_t count = 0; -uint64_t best_count = 0; - -uint64_t digit_best_count = 0; //what the best is -mpz_t digit_count; //how many times we've hit the best - -clock_t start; - -int main(void) -{ - char guess[NUM_DIGITS] = "01"; - mpz_t ans; - mpz_init(ans); - mpz_init(digit_count); - - start = clock(); - - do - { - count = 0; - mpz_set_str(ans, guess, 10); - - while(mpz_cmp_ui(ans, 9) > 0) - { - mpz_mper(ans); - ++count; - } - - if(count > best_count) - { - clock_t found = clock(); - double time_spent = (double)(found - start) / CLOCKS_PER_SEC; - best_count = count; - printf("(%f)\t%lu steps: %s <------\n", time_spent, count, guess); - } - - if (count > digit_best_count) - { - digit_best_count = count; - mpz_set_ui(digit_count, 1); - } - else if(count == digit_best_count) - { - mpz_add_ui(digit_count, digit_count, 1); - } - }while(inc_mper(guess)); -} - -void mpz_mper(mpz_t num) -{ - int sz = gmp_snprintf(tmp_buf, NUM_DIGITS, "%Zd", num); - - if(sz >= NUM_DIGITS) - { - printf("\n=== can't fit num in tmp_buf! ===\n\n"); - return; - } - - mpz_set_ui(num, 1); - - uint_fast16_t i = 0; - while(tmp_buf[i] != '\0') - { - mpz_mul_ui(num, num, tmp_buf[i] - 0x30); - ++i; - } -} - - -bool inc_mper(char *num) -{ - uint_fast16_t i = 0; - - while(num[i] == '9') - { - ++i; - - if(i >= NUM_DIGITS) - { - printf("\n=== ran out of digits ===\n\n"); - return false; - } - } - - if(num[i] == '\0') - { - clock_t found = clock(); - double time_spent = (double)(found - start) / CLOCKS_PER_SEC; - //printf("(%f)\t%lu digits best: %u\n", time_spent, i+1, best_count); - - gmp_printf("(%f) finished %lu digits. %Zd nums with count: %lu\n", time_spent, i, digit_count, digit_best_count); - mpz_set_ui(digit_count, 0); - digit_best_count = 0; - - num[i] = '1'; - } - - char new_num = ++num[i]; - while(i) - { - --i; - num[i] = new_num; - } - - return true; -} diff --git a/proth.c b/proth.c index 5bc895a..2ebc5ea 100755 --- a/proth.c +++ b/proth.c @@ -12,12 +12,12 @@ //#define N_CONST 44200000 /* recent find (k=46157, n=698207)*/ -#define K_CONST 46157 -#define N_CONST 698203 +//#define K_CONST 46157 +//#define N_CONST 698203 /* searching for k=301, n=7360 */ -//#define K_CONST 301 -//#define N_CONST 6500 +#define K_CONST 301 +#define N_CONST 6500 #define NUM_CORES 6 @@ -119,7 +119,7 @@ static void * proth_thread(void *arg) { //get the thread-safe n n = get_n(); - printf("(n=%llu)\n", n); + printf("(n=%lu)\n", n); //calc exp=2^n mpz_pow_ui(two_n, two, n); @@ -132,6 +132,7 @@ static void * proth_thread(void *arg) //mpz_sub_ui(t_exp, p, 1); mpz_divexact_ui(t_exp, p_minus1, 2); +#if 0 //printf("prime check a=2\n"); mpz_powm(t, t_p1, t_exp, proth); mpz_add_ui(t, t, 1); @@ -141,6 +142,7 @@ static void * proth_thread(void *arg) printf("prime with a=2!\n"); break; } +#endif //printf("prime check a=3\n"); mpz_powm(t, t_p2, t_exp, proth); @@ -180,4 +182,6 @@ static void * proth_thread(void *arg) mpz_clear(t); pthread_mutex_unlock(&found_lock); + + return NULL; } diff --git a/proth_test.c b/proth_test.c deleted file mode 100755 index d4f36ca..0000000 --- a/proth_test.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include -#include -#include - -#include - - - -int main(void) -{ - mpz_t p; - mpz_t exp; - mpz_t base; - mpz_t rop; - - mpz_init(p); - mpz_init(exp); - mpz_init(base); - mpz_init(rop); - - mpz_set_ui(base, 5); - mpz_set_ui(exp, (13-1)/2); - mpz_set_ui(p, 13); - - mpz_powm(rop, base, exp, p); - - gmp_printf("%Zd\n", rop); - - mpz_clear(p); - mpz_clear(exp); - mpz_clear(base); - mpz_clear(rop); -} -- 2.17.1