Smarandache-Wellin primes: Difference between revisions
→{{header|C}}: Changed in line with Wren solution of which it is a translation.
(→{{header|Go}}: Changed in line with Wren solution of which it is a translation.) |
(→{{header|C}}: Changed in line with Wren solution of which it is a translation.) |
||
Line 33:
=={{header|C}}==
{{trans|Wren}}
{{libheader|GMP}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
Line 39:
#include <string.h>
#include <stdint.h>
#include <gmp.h>
bool *sieve(int limit) {
Line 74 ⟶ 61:
return c;
}
const char *ord(int count) {
return (count == 1) ? "st" :
(count == 2) ? "nd" :
(count == 3) ? "rd" : "th";
}
int main() {
bool *c = sieve(
char sw[
char tmp[20];
int
mpz_t n;
printf("The known Smarandache-Wellin primes are:\n");
while (count < 8) {
while (c[++p]);
sprintf(tmp, "%d", p);
strcat(sw, tmp);
if (
count++;
size_t le = strlen(sw);
char sws[44];
if (le < 5) {
strcpy(sws, sw);
} else {
strncpy(sws, sw, 20);
strcpy(sws + 20, "...");
strncpy(sws + 23, sw + le - 20, 21);
}
printf("%2d%s: index %4d digits %4ld last prime %5d -> %s\n", count, ord(count), ix+1, le, p, sws);
}
ix++;
}
printf("\nThe first 20 Derived Smarandache-Wellin primes are:\n");
int freqs[10] = {0};
count = 0;
p = 1;
while (count < 20) {
while (c[++p]);
sprintf(tmp, "%d", p);
for (i = 0; i < strlen(tmp); ++i) freqs[tmp[i]-48]++;
char dsw[
for (i = 0; i < 10; ++i) {
sprintf(tmp, "%d", freqs[i]);
strcat(dsw, tmp);
}
int
for (i = 0; i < strlen(dsw); ++i) {
if (dsw[i] != '0') {
break;
}
}
if (mpz_probab_prime_p(n, 15) > 0) {
count++;
}
ix++;
}
free(c);
Line 188 ⟶ 131:
{{out}}
<pre>
The
6th: index 342 digits 1171 last prime 2297 -> 23571113171923293137...22732281228722932297
7th: index 435 digits 1543 last prime 3037 -> 23571113171923293137...30013011301930233037
8th: index 1429 digits 5719 last prime 11927 -> 23571113171923293137...11903119091192311927
The first 20 Derived Smarandache-Wellin primes are:
1st: index 32 prime 4194123321127
2nd: index 72 prime 547233879626521
3rd: index 73 prime 547233979727521
4th: index 134 prime 13672766322929571043
5th: index 225 prime 3916856106393739943689
6th: index 303 prime 462696313560586013558131
7th: index 309 prime 532727113760586013758133
8th: index 363 prime 6430314317473636515467149
9th: index 462 prime 8734722823685889120488197
10th: index 490 prime 9035923128899919621189209
11th: index 495 prime 9036023329699969621389211
12th: index 522 prime 9337023533410210710923191219
13th: index 538 prime 94374237357103109113243102223
14th: index 624 prime 117416265406198131121272110263
15th: index 721 prime 141459282456260193137317129313
16th: index 738 prime 144466284461264224139325131317
17th: index 790 prime 156483290479273277162351153339
18th: index 852 prime 164518312512286294233375158359
19th: index 1087 prime 208614364610327343341589284471
20th: index 1188 prime 229667386663354357356628334581
</pre>
|