-#include <time.h>
#include <stdio.h>
#include <string.h>
-#include <stdint.h>
#include <stdbool.h>
-#include <gmp.h>
+#include <time.h>
+#include <gmp.h>
-bool is_prime(mpz_t p);
-bool is_prime2(mpz_t p);
+/* the real test */
+//#define K_CONST 21181
+//#define N_CONST 44200000
-clock_t start;
+/* recent find (k=46157, n=698207)*/
+//#define K_CONST 46157
+//#define N_CONST 698206
+/* searching for k=301, n=7360 */
+#define K_CONST 301
+#define N_CONST 6500
-//ints for is_prime test
mpz_t t_exp;
mpz_t t_p1;
mpz_t t_p2;
mpz_t t_p3;
mpz_t t;
+time_t start;
+time_t end;
int main(void)
{
+ time(&start);
+
mpz_t exp;
mpz_t p;
mpz_t k;
mpz_set_ui(t_p2, 3);
mpz_set_ui(t_p3, 5);
- //start = clock();
-
-
//proth number is k*2^n+1
- //check where k is 19
- mpz_set_ui(k, 46157);
+ mpz_set_ui(k, K_CONST);
- //calc 2^n
+ //two is our base
mpz_set_ui(base, 2);
- for(unsigned long int n = 698200; ; n++)
+ unsigned long int n = N_CONST;
+ while (1)
{
+ printf("(n=%llu)\n", n);
+
+ //calc 2^n
mpz_pow_ui(exp, base, n);
//p=k*2^n
//p+=1
mpz_add_ui(p, p, 1);
- gmp_printf("(n=%d)\n%Zd\n\n", n, p);
- if(is_prime2(p) == true)
+ //prime check exponent is (p-1)/2
+ mpz_sub_ui(t_exp, p, 1);
+ mpz_divexact_ui(t_exp, t_exp, 2);
+
+ //printf("prime check a=2\n");
+ mpz_powm(t, t_p1, t_exp, p);
+ mpz_add_ui(t, t, 1);
+ //printf("cmp1\n");
+ if(mpz_cmp(t, p) == 0)
+ {
+ printf("prime with a=2!\n");
+ break;
+ }
+
+ //printf("prime check a=3\n");
+ mpz_powm(t, t_p2, t_exp, p);
+ mpz_add_ui(t, t, 1);
+ //printf("cmp2\n");
+ if(mpz_cmp(t, p) == 0)
{
- printf(" <--- prime\n");
- return 1;
+ printf("prime with a=3!\n");
+ break;
}
- printf("\n");
+
+ //printf("prime check a=5\n");
+ mpz_powm(t, t_p3, t_exp, p);
+ mpz_add_ui(t, t, 1);
+ //printf("cmp3\n");
+ if(mpz_cmp(t, p) == 0)
+ {
+ printf("prime with a=5!\n");
+ break;
+ }
+
+ n++;
}
- //free p
+ //proth number is k*2^n+1
+ gmp_printf("%Zd*2^%llu+1 is prime!\n", k, n);
+
+ //free
mpz_clear(p);
-}
-bool is_prime(mpz_t p)
-{
- //exponent is (p-1)/2
- //printf("exp\n");
- mpz_sub_ui(t_exp, p, 1);
- mpz_divexact_ui(t_exp, t_exp, 2);
- unsigned long int ui_exp = mpz_get_ui(t_exp);
-
- //printf("pow1\n");
- mpz_pow_ui(t, t_p1, ui_exp);
- mpz_add_ui(t, t, 1);
- //printf("div1\n");
- if(mpz_divisible_p(t, p) != 0)
- return true;
-
- //printf("pow2\n");
- mpz_pow_ui(t, t_p2, ui_exp);
- mpz_add_ui(t, t, 1);
- //printf("div2\n");
- if(mpz_divisible_p(t, p) != 0)
- return true;
-
- //printf("pow3\n");
- mpz_pow_ui(t, t_p3, ui_exp);
- mpz_add_ui(t, t, 1);
- //printf("div3\n");
- if(mpz_divisible_p(t, p) != 0)
- return true;
-
- return false;
-}
+ mpz_clear(k);
+ mpz_clear(exp);
+ mpz_clear(base);
-bool is_prime2(mpz_t p)
-{
- //exponent is (p-1)/2
- printf("exp\n");
- mpz_sub_ui(t_exp, p, 1);
- mpz_divexact_ui(t_exp, t_exp, 2);
-
- printf("pow1\n");
- mpz_powm(t, t_p1, t_exp, p);
- mpz_add_ui(t, t, 1);
- printf("cmp1\n");
- if(mpz_cmp(t, p) == 0)
- return true;
-
- printf("pow2\n");
- mpz_powm(t, t_p2, t_exp, p);
- mpz_add_ui(t, t, 1);
- printf("cmp2\n");
- if(mpz_cmp(t, p) == 0)
- return true;
-
- printf("pow3\n");
- mpz_powm(t, t_p3, t_exp, p);
- mpz_add_ui(t, t, 1);
- printf("cmp3\n");
- if(mpz_cmp(t, p) == 0)
- return true;
-
- return false;
+ mpz_clear(t_exp);
+ mpz_clear(t_p1);
+ mpz_clear(t_p2);
+ mpz_clear(t_p3);
+ mpz_clear(t);
+
+ time(&end);
+ printf("total time: %f seconds\n", difftime(end, start));
}