10 /* the real test (max complete n=36576908*/
11 //#define K_CONST 21181
12 //#define N_CONST 44200000
13 //#define N_CONST 36576908
15 /* recent find (k=46157, n=698207)*/
16 //#define K_CONST 46157
17 //#define N_CONST 698203
19 /* searching for k=301, n=7360 */
25 static void * proth_thread(void *arg
);
27 //mutex protected, thread safe n
28 unsigned long int n_val
= N_CONST
;
29 pthread_mutex_t n_lock
;
30 pthread_mutex_t found_lock
;
38 //array to keep track of the thread id's
39 pthread_t thread_ids
[NUM_CORES
];
42 pthread_mutex_init(&n_lock
, NULL
);
43 pthread_mutex_init(&found_lock
, NULL
);
46 for(int i
= 0; i
< NUM_CORES
; i
++)
48 pthread_create(&thread_ids
[i
], NULL
, &proth_thread
, NULL
);
51 //lock main thread until prime is found
52 pthread_mutex_lock(&found_lock
);
53 pthread_mutex_lock(&found_lock
);
56 for(int i
= 0; i
< NUM_CORES
; i
++)
58 pthread_cancel(thread_ids
[i
]);
62 pthread_mutex_destroy(&n_lock
);
63 pthread_mutex_destroy(&found_lock
);
66 printf("total time: %f seconds\n", difftime(end
, start
));
71 pthread_mutex_lock(&n_lock
);
72 unsigned long n
= n_val
;
74 pthread_mutex_unlock(&n_lock
);
78 static void * proth_thread(void *arg
)
80 //proth number is p=k*2^n+1
83 // variables to calculate the proth number: p=k*2^n+1
84 mpz_t proth
; // the proth number
85 mpz_t p_minus1
; // the proth number minus 1
90 // variables to test the primality: t = a^((p-1)/2) % p
96 //initialize the variables
110 //proth number is p=k*2^n+1
112 mpz_set_ui(k
, K_CONST
);
118 //get the thread-safe n
120 printf("(n=%lu)\n", n
);
122 //Step 1: Caluclate the Proth Number
124 mpz_pow_ui(two_n
, two
, n
);
126 mpz_mul(p_minus1
, k
, two_n
);
127 //calc p=k*2^n+1 (this is our proth number)
128 mpz_add_ui(proth
, p_minus1
, 1);
130 //Step 2: Check if the Proth Number is Prime
131 //the prime check exponent is (p-1)/2
132 mpz_divexact_ui(a_exp
, p_minus1
, 2);
134 //printf("prime check a=3\n");
135 mpz_powm(t
, a1
, a_exp
, proth
);
137 if(mpz_cmp(t
, proth
) == 0)
139 printf("prime with a=3!\n");
143 //printf("prime check a=5\n");
144 mpz_powm(t
, a2
, a_exp
, proth
);
146 if(mpz_cmp(t
, proth
) == 0)
148 printf("prime with a=5!\n");
153 //proth number is k*2^n+1
154 gmp_printf("%Zd*2^%llu+1 is prime!\n", k
, n
);
167 FILE *fp
= fopen("proth_file", "w");
168 fprintf(fp
, "%lu\n", n
);
171 pthread_mutex_unlock(&found_lock
);