Wagstaff primes: Difference between revisions

Added C
(Added C)
Line 159:
call Wagstaff(9) #BASIC-256 does not allow larger numbers
end</syntaxhighlight>
 
=={{header|C}}==
{{libheader|GMP}}
This takes about 24 seconds to find the first 24 Wagstaff primes but 589 seconds to find the first 29.
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <gmp.h>
 
int main() {
const int limit = 29;
int count = 0;
char tmp[40];
mpz_t p, w;
mpz_init_set_ui(p, 1);
mpz_init(w);
while (count < limit) {
mpz_nextprime(p, p);
mpz_set_ui(w, 1);
unsigned long ulp = mpz_get_ui(p);
mpz_mul_2exp(w, w, ulp);
mpz_add_ui(w, w, 1);
mpz_tdiv_q_ui(w, w, 3);
if (mpz_probab_prime_p(w, 15) > 0) {
++count;
char *ws = mpz_get_str(NULL, 10, w);
size_t le = strlen(ws);
if (le < 33) {
strcpy(tmp, ws);
} else {
strncpy(tmp, ws, 15);
strcpy(tmp + 15, "...");
strncpy(tmp + 18, ws + le - 15, 16);
}
printf("%5lu: %s", ulp, tmp);
if (le >=34) printf( " (%ld digits)", le);
printf("\n");
}
}
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
3: 3
5: 11
7: 43
11: 683
13: 2731
17: 43691
19: 174763
23: 2796203
31: 715827883
43: 2932031007403
61: 768614336404564651
79: 201487636602438195784363
101: 845100400152152934331135470251
127: 567137278201564...101238628035243 (38 digits)
167: 623574031927851...838653121833643 (50 digits)
191: 104618362256444...574077339085483 (58 digits)
199: 267823007376498...963798805883563 (60 digits)
313: 556246623937737...099018130434731 (94 digits)
347: 955624423329196...712921903606443 (104 digits)
701: 350675726769891...862167823854251 (211 digits)
1709: 961925272487010...857299070528171 (514 digits)
2617: 208150470990263...847933435947691 (788 digits)
3539: 737960982013072...304686486497963 (1065 digits)
5807: 401849623734300...466686663568043 (1748 digits)
10501: 435374724597165...340431558126251 (3161 digits)
10691: 683222859828090...209259644365483 (3218 digits)
11279: 692149388152358...081084271176363 (3395 digits)
12391: 385083595083686...967789680077483 (3730 digits)
14479: 136831460986221...678801865288363 (4359 digits)
</pre>
 
=={{header|C++}}==
9,485

edits