11 //#define K_CONST 21181
12 //#define N_CONST 44200000
14 /* recent find (k=46157, n=698207)*/
16 #define N_CONST 698203
18 /* searching for k=301, n=7360 */
20 //#define N_CONST 6500
24 static void * proth_thread(void *arg
);
26 //mutex protected, thread safe n
27 unsigned long int n_val
= N_CONST
;
28 pthread_mutex_t n_lock
;
29 pthread_mutex_t found_lock
;
37 //array to keep track of the thread id's
38 pthread_t thread_ids
[NUM_CORES
];
41 pthread_mutex_init(&n_lock
, NULL
);
42 pthread_mutex_init(&found_lock
, NULL
);
45 for(int i
= 0; i
< NUM_CORES
; i
++)
47 pthread_create(&thread_ids
[i
], NULL
, &proth_thread
, NULL
);
50 //lock main thread until prime is found
51 pthread_mutex_lock(&found_lock
);
52 pthread_mutex_lock(&found_lock
);
55 for(int i
= 0; i
< NUM_CORES
; i
++)
57 pthread_cancel(thread_ids
[i
]);
61 pthread_mutex_destroy(&n_lock
);
62 pthread_mutex_destroy(&found_lock
);
65 printf("total time: %f seconds\n", difftime(end
, start
));
70 pthread_mutex_lock(&n_lock
);
71 unsigned long n
= n_val
;
73 pthread_mutex_unlock(&n_lock
);
77 static void * proth_thread(void *arg
)
79 // variables to test the primality
92 // variables to calculate the proth number p=k*2^n+1
93 mpz_t proth
; //the proth number
94 mpz_t p_minus1
; // proth number minus 1
110 //proth number is p=k*2^n+1
111 mpz_set_ui(k
, K_CONST
);
120 //get the thread-safe n
122 printf("(n=%llu)\n", n
);
125 mpz_pow_ui(two_n
, two
, n
);
127 mpz_mul(p_minus1
, k
, two_n
);
128 //calc p=k*2^n+1 (this is our proth number)
129 mpz_add_ui(proth
, p_minus1
, 1);
131 //the prime check exponent is (p-1)/2
132 //mpz_sub_ui(t_exp, p, 1);
133 mpz_divexact_ui(t_exp
, p_minus1
, 2);
135 //printf("prime check a=2\n");
136 mpz_powm(t
, t_p1
, t_exp
, proth
);
139 if(mpz_cmp(t
, proth
) == 0)
141 printf("prime with a=2!\n");
145 //printf("prime check a=3\n");
146 mpz_powm(t
, t_p2
, t_exp
, proth
);
149 if(mpz_cmp(t
, proth
) == 0)
151 printf("prime with a=3!\n");
155 //printf("prime check a=5\n");
156 mpz_powm(t
, t_p3
, t_exp
, proth
);
159 if(mpz_cmp(t
, proth
) == 0)
161 printf("prime with a=5!\n");
166 //proth number is k*2^n+1
167 gmp_printf("%Zd*2^%llu+1 is prime!\n", k
, n
);
182 pthread_mutex_unlock(&found_lock
);