Smarandache-Wellin primes: Difference between revisions
Content added Content deleted
(Added Go) |
(Added C) |
||
Line 30: | Line 30: | ||
<br> |
<br> |
||
=={{header|C}}== |
|||
{{trans|Wren}} |
|||
===Basic=== |
|||
<syntaxhighlight lang="c">#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <stdbool.h> |
|||
#include <string.h> |
|||
#include <stdint.h> |
|||
bool isPrime(uint64_t n) { |
|||
if (n < 2) return false; |
|||
if (n%2 == 0) return n == 2; |
|||
if (n%3 == 0) return n == 3; |
|||
uint64_t d = 5; |
|||
while (d*d <= n) { |
|||
if (n%d == 0) return false; |
|||
d += 2; |
|||
if (n%d == 0) return false; |
|||
d += 4; |
|||
} |
|||
return true; |
|||
} |
|||
bool *sieve(int limit) { |
|||
int i, p; |
|||
limit++; |
|||
// True denotes composite, false denotes prime. |
|||
bool *c = calloc(limit, sizeof(bool)); // all false by default |
|||
c[0] = true; |
|||
c[1] = true; |
|||
for (i = 4; i < limit; i += 2) c[i] = true; |
|||
p = 3; // Start from 3. |
|||
while (true) { |
|||
int p2 = p * p; |
|||
if (p2 >= limit) break; |
|||
for (i = p2; i < limit; i += 2 * p) c[i] = true; |
|||
while (true) { |
|||
p += 2; |
|||
if (!c[p]) break; |
|||
} |
|||
} |
|||
return c; |
|||
} |
|||
int main() { |
|||
bool *c = sieve(400); |
|||
char sw[100] = ""; |
|||
char tmp[20]; |
|||
int swp[3]; |
|||
int count = 0, p = 1, i; |
|||
while (count < 3) { |
|||
while (c[++p]); |
|||
sprintf(tmp, "%d", p); |
|||
strcat(sw, tmp); |
|||
int n = atoi(sw); |
|||
if (isPrime(n)) swp[count++] = n; |
|||
} |
|||
printf("The first 3 Smarandache-Wellin primes are:\n"); |
|||
for (i = 0; i < 3; ++i) printf("%d ", swp[i]); |
|||
printf("\n"); |
|||
int freqs[10] = {0}; |
|||
uint64_t dswp[3]; |
|||
count = 0; |
|||
p = 1; |
|||
while (count < 3) { |
|||
while (c[++p]); |
|||
sprintf(tmp, "%d", p); |
|||
for (i = 0; i < strlen(tmp); ++i) freqs[tmp[i]-48]++; |
|||
char dsw[20] = ""; |
|||
for (i = 0; i < 10; ++i) { |
|||
sprintf(tmp, "%d", freqs[i]); |
|||
strcat(dsw, tmp); |
|||
} |
|||
int ix = -1; |
|||
for (i = 0; i < strlen(dsw); ++i) { |
|||
if (dsw[i] != '0') { |
|||
ix = i; |
|||
break; |
|||
} |
|||
} |
|||
uint64_t dn = atoll(dsw + ix); |
|||
if (isPrime(dn)) dswp[count++] = dn; |
|||
} |
|||
printf("\nThe first 3 Derived Smarandache-Wellin primes are:\n"); |
|||
for (i = 0; i < 3; ++i) printf("%ld ", dswp[i]); |
|||
printf("\n"); |
|||
free(c); |
|||
return 0; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 3 Smarandache-Wellin primes are: |
|||
2 23 2357 |
|||
The first 3 Derived Smarandache-Wellin primes are: |
|||
4194123321127 547233879626521 547233979727521 |
|||
</pre> |
|||
===Stretch=== |
|||
{{libheader|GMP}} |
|||
<syntaxhighlight lang="c">#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <stdbool.h> |
|||
#include <string.h> |
|||
#include <stdint.h> |
|||
#include <gmp.h> |
|||
bool *sieve(int limit) { |
|||
int i, p; |
|||
limit++; |
|||
// True denotes composite, false denotes prime. |
|||
bool *c = calloc(limit, sizeof(bool)); // all false by default |
|||
c[0] = true; |
|||
c[1] = true; |
|||
for (i = 4; i < limit; i += 2) c[i] = true; |
|||
p = 3; // Start from 3. |
|||
while (true) { |
|||
int p2 = p * p; |
|||
if (p2 >= limit) break; |
|||
for (i = p2; i < limit; i += 2 * p) c[i] = true; |
|||
while (true) { |
|||
p += 2; |
|||
if (!c[p]) break; |
|||
} |
|||
} |
|||
return c; |
|||
} |
|||
int main() { |
|||
bool *c = sieve(12000); |
|||
char sw[6000] = ""; |
|||
char tmp[20]; |
|||
int count = 0, p = 1, ix = 0; |
|||
mpz_t n; |
|||
mpz_init(n); |
|||
printf("The 4th to the 8th Smarandache-Wellin primes are:\n"); |
|||
while (count < 8) { |
|||
while (c[++p]); |
|||
sprintf(tmp, "%d", p); |
|||
strcat(sw, tmp); |
|||
mpz_set_str(n, sw, 10); |
|||
if (mpz_probab_prime_p(n, 15) > 0) { |
|||
count++; |
|||
if (count > 3) { |
|||
printf("%dth: index %4d digits %4ld last prime %5d\n", count, ix+1, strlen(sw), p); |
|||
} |
|||
} |
|||
ix++; |
|||
} |
|||
free(c); |
|||
return 0; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The 4th to the 8th Smarandache-Wellin primes are: |
|||
4th: index 128 digits 355 last prime 719 |
|||
5th: index 174 digits 499 last prime 1033 |
|||
6th: index 342 digits 1171 last prime 2297 |
|||
7th: index 435 digits 1543 last prime 3037 |
|||
8th: index 1429 digits 5719 last prime 11927 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |