Smarandache-Wellin primes: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m ("4..20" was only from two-part implementations, task shd just say "first 20")
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(23 intermediate revisions by 12 users not shown)
Line 1:
{{draft task}}
;Definitions
A '''Smarandache-Wellin number''' (S-W number for short) is an integer that in a given base is the concatenation of the first n prime numbers written in that base. A base of 10 will be assumed for this task.
Line 33:
=={{header|C}}==
{{trans|Wren}}
{{libheader|GMP}}
===Basic===
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
Line 39:
#include <string.h>
#include <stdint.h>
#include <gmp.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) {
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(40012000);
char sw[1006000] = "";
char tmp[20];
int swp[3]count = 0, p = 1, ix = 0, i;
mpz_t n;
int count = 0, p = 1, i;
while mpz_init(count < 3n) {;
printf("The known Smarandache-Wellin primes are:\n");
while (count < 8) {
while (c[++p]);
sprintf(tmp, "%d", p);
strcat(sw, tmp);
int mpz_set_str(n, =sw, atoi(sw10);
if (isPrimempz_probab_prime_p(n), 15) swp[count++]> =0) n;{
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("The first 3 Smarandache-Wellin primes are:\n");
for (i = 0; i < 3; ++i) printf("%d ", swp[i]);
printf("\n");
 
printf("\nThe first 20 Derived Smarandache-Wellin primes are:\n");
int freqs[10] = {0};
uint64_t dswp[3];
count = 0;
p = 1;
whileix (count= < 3) {0;
while (count < 20) {
while (c[++p]);
sprintf(tmp, "%d", p);
for (i = 0; i < strlen(tmp); ++i) freqs[tmp[i]-48]++;
char dsw[2040] = "";
for (i = 0; i < 10; ++i) {
sprintf(tmp, "%d", freqs[i]);
strcat(dsw, tmp);
}
int ixidx = -1;
for (i = 0; i < strlen(dsw); ++i) {
if (dsw[i] != '0') {
ixidx = i;
break;
}
}
uint64_tmpz_set_str(n, dn = atoll(dsw + ixidx, 10);
if (isPrimempz_probab_prime_p(dn)n, 15) dswp[count++]> =0) dn;{
count++;
printf("%2d%s: index %4d prime %s\n", count, ord(count), ix+1, dsw + idx);
}
ix++;
}
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;
Line 124 ⟶ 131:
{{out}}
<pre>
The first 3known Smarandache-Wellin primes are:
1st: index 1 digits 1 last prime 2 -> 2
2 23 2357
2nd: index 2 digits 2 last prime 3 -> 23
3rd: index 4 digits 4 last prime 7 -> 2357
4th: index 128 digits 355 last prime 719 -> 23571113171923293137...73677683691701709719
5th: index 174 digits 499 last prime 1033 -> 23571113171923293137...10131019102110311033
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 320 Derived Smarandache-Wellin primes are:
1st: index 32 prime 4194123321127
4194123321127 547233879626521 547233979727521
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>
 
===Stretch={{header|C++}}==
{{libheader|GMP}}
{{libheader|Primesieve}}
<syntaxhighlight lang="c">#include <stdio.h>
<syntaxhighlight lang="cpp">#include <stdlib.hiomanip>
#include <stdbool.hiostream>
#include <string.h>
#include <stdint.h>
#include <gmp.h>
 
#include <primesieve.hpp>
bool *sieve(int limit) {
 
int i, p;
#include <gmpxx.h>
limit++;
 
// True denotes composite, false denotes prime.
using big_int = mpz_class;
bool *c = calloc(limit, sizeof(bool)); // all false by default
 
c[0] = true;
class sw_number_generator {
c[1] = true;
public:
for (i = 4; i < limit; i += 2) c[i] = true;
sw_number_generator() { next(); }
p = 3; // Start from 3.
whilevoid next(true) {;
const std::string& number() const { return number_; }
int p2 = p * p;
uint64_t prime() const { return prime_; }
if (p2 >= limit) break;
int index() const { return index_; }
for (i = p2; i < limit; i += 2 * p) c[i] = true;
std::string derived_number() const;
while (true) {
 
p += 2;
private:
if (!c[p]) break;
primesieve::iterator pi_;
std::string number_;
uint64_t prime_;
int index_ = 0;
};
 
void sw_number_generator::next() {
++index_;
prime_ = pi_.next_prime();
number_ += std::to_string(prime_);
}
 
std::string sw_number_generator::derived_number() const {
int count[10] = {};
for (char c : number_)
++count[c - '0'];
std::string str;
for (int i = 0; i < 10; ++i) {
if (!str.empty() || count[i] != 0)
str += std::to_string(count[i]);
}
return str;
}
 
bool is_probably_prime(const big_int& n) {
return mpz_probab_prime_p(n.get_mpz_t(), 25) != 0;
}
 
std::string abbreviate(std::string str, size_t max_digits) {
size_t len = str.size();
if (len > max_digits)
str.replace(max_digits / 2, len - max_digits, "...");
return str;
}
 
void print_sw_primes() {
std::cout
<< "Known Smarandache-Wellin primes:\n\n"
<< " |Index|Digits|Last prime| Smarandache-Wellin prime\n"
<< "----------------------------------------------------------------------\n";
sw_number_generator swg;
for (int n = 1; n <= 8; swg.next()) {
std::string num = swg.number();
if (is_probably_prime(big_int(num))) {
std::cout << std::setw(2) << n << "|"
<< std::setw(5) << swg.index() << "|"
<< std::setw(6) << num.size() << "|"
<< std::setw(10) << swg.prime() << "|"
<< std::setw(43) << abbreviate(num, 40) << '\n';
++n;
}
}
return c;
}
 
void print_derived_sw_primes(int count) {
int main() {
std::cout << "First " << count << " Derived Smarandache-Wellin primes:\n\n"
bool *c = sieve(12000);
<< " |Index|Derived Smarandache-Wellin prime\n"
char sw[6000] = "";
<< "-----------------------------------------\n";
char tmp[20];
sw_number_generator swg;
int count = 0, p = 1, ix = 0;
for (int n = 1; n <= count; swg.next()) {
mpz_t n;
std::string num = swg.derived_number();
mpz_init(n);
if (is_probably_prime(big_int(num))) {
printf("The 4th to the 8th Smarandache-Wellin primes are:\n");
std::cout << std::setw(2) << n << "|"
while (count < 8) {
<< std::setw(5) << swg.index() << "|"
while (c[++p]);
<< std::setw(32) << num << '\n';
sprintf(tmp, "%d", p);
strcat(sw, tmp) ++n;
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;
int main() {
print_sw_primes();
std::cout << '\n';
print_derived_sw_primes(20);
}</syntaxhighlight>
 
{{out}}
<pre>
The 4th to the 8thKnown Smarandache-Wellin primes are:
 
4th: index 128 digits 355 last prime 719
|Index|Digits|Last prime| Smarandache-Wellin prime
5th: index 174 digits 499 last prime 1033
----------------------------------------------------------------------
6th: index 342 digits 1171 last prime 2297
1| 1| 1| 2| 2
7th: index 435 digits 1543 last prime 3037
2| 2| 2| 3| 23
8th: index 1429 digits 5719 last prime 11927
3| 4| 4| 7| 2357
4| 128| 355| 719|23571113171923293137...73677683691701709719
5| 174| 499| 1033|23571113171923293137...10131019102110311033
6| 342| 1171| 2297|23571113171923293137...22732281228722932297
7| 435| 1543| 3037|23571113171923293137...30013011301930233037
8| 1429| 5719| 11927|23571113171923293137...11903119091192311927
 
First 20 Derived Smarandache-Wellin primes:
 
|Index|Derived Smarandache-Wellin prime
-----------------------------------------
1| 32| 4194123321127
2| 72| 547233879626521
3| 73| 547233979727521
4| 134| 13672766322929571043
5| 225| 3916856106393739943689
6| 303| 462696313560586013558131
7| 309| 532727113760586013758133
8| 363| 6430314317473636515467149
9| 462| 8734722823685889120488197
10| 490| 9035923128899919621189209
11| 495| 9036023329699969621389211
12| 522| 9337023533410210710923191219
13| 538| 94374237357103109113243102223
14| 624| 117416265406198131121272110263
15| 721| 141459282456260193137317129313
16| 738| 144466284461264224139325131317
17| 790| 156483290479273277162351153339
18| 852| 164518312512286294233375158359
19| 1087| 208614364610327343341589284471
20| 1188| 229667386663354357356628334581
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func nextprim num .
while isprim num = 0
num += 1
.
return num
.
len digs[] 10
prim = 1
while len prsw[] < 3 or len prdsw[] < 3
prim = nextprim (prim + 1)
h = prim
h[] = [ ]
while h > 0
d = h mod 10
digs[d + 1] += 1
h[] &= d
h = h div 10
.
for i = len h[] downto 1
sw = sw * 10 + h[i]
.
if isprim sw = 1
prsw[] &= sw
.
dsw = 0
for i to 10
if digs[i] = 0
h = 10
else
h = 1 + floor log10 digs[i]
h = pow 10 h
.
dsw = dsw * h + digs[i]
.
if isprim dsw = 1
prdsw[] &= dsw
.
.
print prsw[]
print prdsw[]
</syntaxhighlight>
 
{{out}}
<pre>
[ 2 23 2357 ]
[ 4194123321127 547233879626521 547233979727521 ]
</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Smarandache-Wellin primes. Nigel Galloway: April 3rd., 2023
let izP(g,_,_,_)=let mutable g=System.Numerics.BigInteger.Parse g
Open.Numeric.Primes.MillerRabin.IsProbablePrime &g
let fN g="0123456789"+g|>Seq.countBy id|>Seq.map(fun(_,g)->string(g-1))|>Seq.fold((+))""
let sw()=primes32()|>Seq.scan(fun(n,i,g,e)l->let a=string l in (n+a,l,g+1,e+(String.length a)))("",0,0,0)|>Seq.skip 1
sw()|>Seq.filter izP|>Seq.take 3|>Seq.iter(fun(i,_,_,_)->printf "%s " i); printfn ""
sw()|>Seq.filter izP|>Seq.take 7|>Seq.iter(fun(_,n,g,l)->printfn "index %4d: digits %4d last prime %d" g l n); printfn ""
sw()|>Seq.map(fun(i,g,e,l)->(fN i,g,e,l))|>Seq.filter izP|>Seq.take 20|>Seq.iter(fun(n,_,g,_)->printfn $"index %4d{g}: prime %s{n}")
</syntaxhighlight>
{{out}}
<pre>
2 23 2357
index 1: digits 1 last prime 2
index 2: digits 2 last prime 3
index 4: digits 4 last prime 7
index 128: digits 355 last prime 719
index 174: digits 499 last prime 1033
index 342: digits 1171 last prime 2297
index 435: digits 1543 last prime 3037
index 32: prime 4194123321127
index 72: prime 547233879626521
index 73: prime 547233979727521
index 134: prime 13672766322929571043
index 225: prime 3916856106393739943689
index 303: prime 462696313560586013558131
index 309: prime 532727113760586013758133
index 363: prime 6430314317473636515467149
index 462: prime 8734722823685889120488197
index 490: prime 9035923128899919621189209
index 495: prime 9036023329699969621389211
index 522: prime 9337023533410210710923191219
index 538: prime 94374237357103109113243102223
index 624: prime 117416265406198131121272110263
index 721: prime 141459282456260193137317129313
index 738: prime 144466284461264224139325131317
index 790: prime 156483290479273277162351153339
index 852: prime 164518312512286294233375158359
index 1087: prime 208614364610327343341589284471
index 1188: prime 229667386663354357356628334581
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
===Basic===
{{libheader|Go-rcu}}
{{libheader|GMP(Go wrapper)}}
We need to use the above GMP wrapper to match Wren's time of about 35.5 seconds as Go's native big.Int type is far slower.
<syntaxhighlight lang="go">package main
 
import (
"fmt"
big "github.com/ncw/gmp"
"rcu"
"strconv"
"strings"
)
 
func ord(count int) string {
if count == 1 {
return "st"
}
if count == 2 {
return "nd"
}
if count == 3 {
return "rd"
}
return "th"
}
 
func main() {
primes := rcu.Primes(40012000)
sw := ""
var swp []int
count := 0
i := 0
forn count:= < 3 {new(big.Int)
fmt.Println("The known Smarandache-Wellin primes are:")
for count < 8 {
sw += strconv.Itoa(primes[i])
n, _ := strconv.AtoiSetString(sw, 10)
if rcun.IsPrimeProbablyPrime(n15) {
swp = append(swp, n)
count++
sws := sw
le := len(sws)
if le > 4 {
sws = sws[0:20] + "..." + sws[le-20:le]
}
fmt.Printf("%d%s: index %4d digits %4d last prime %5d -> %s\n", count, ord(count), i+1, len(sw), primes[i], sws)
}
i++
}
fmt.Println("The first 3 Smarandache-Wellin primes are:")
fmt.Printf("%v\n", swp)
 
fmt.Println("\nThe first 20 Derived Smarandache-Wellin primes are:")
freqs := make([]int, 10)
var dswp []int
count = 0
i = 0
for count < 320 {
p := strconv.Itoa(primes[i])
for _, d := range p {
Line 242 ⟶ 472:
}
dsw = strings.TrimLeft(dsw, "0")
dn, _ := strconvn.AtoiSetString(dsw, 10)
if rcun.IsPrimeProbablyPrime(dn15) {
dswp = append(dswp, dn)
count++
fmt.Printf("%2d%s: index %4d prime %v\n", count, ord(count), i+1, n)
}
i++
}
fmt.Println("\nThe first 3 Derived Smarandache-Wellin primes are:")
fmt.Printf("%v\n", dswp)
}</syntaxhighlight>
 
{{out}}
<pre>
The first 3known Smarandache-Wellin primes are:
1st: index 1 digits 1 last prime 2 -> 2
[2 23 2357]
2nd: index 2 digits 2 last prime 3 -> 23
3rd: index 4 digits 4 last prime 7 -> 2357
4th: index 128 digits 355 last prime 719 -> 23571113171923293137...73677683691701709719
5th: index 174 digits 499 last prime 1033 -> 23571113171923293137...10131019102110311033
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 320 Derived Smarandache-Wellin primes are:
1st: index 32 prime 4194123321127
[4194123321127 547233879626521 547233979727521]
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>
 
===Stretch={{header|J}}==
<syntaxhighlight lang="j"> derive=. [: <:@#/.~ i.@10 , ]
{{libheader|GMP(Go wrapper)}}
digits=. ;@:(<@("."0@":)"0)
We need to use the above GMP wrapper to match Wren's time of about 35.5 seconds as Go's native big.Int type is far slower.
<syntaxhighlight lang="go">package main
(#~ 1&p:) 10&#.@digits\ p: i. 10
2 23 2357
 
(#~ 1 p: {:"1) (# , 10x&#.@digits@derive@digits)\ p: i. 1200
import (
32 4194123321127
"fmt"
72 547233879626521
big "github.com/ncw/gmp"
73 547233979727521
"rcu"
134 13672766322929571043
"strconv"
225 3916856106393739943689
)
303 462696313560586013558131
309 532727113760586013758133
363 6430314317473636515467149
462 8734722823685889120488197
490 9035923128899919621189209
495 9036023329699969621389211
522 9337023533410210710923191219
538 94374237357103109113243102223
624 117416265406198131121272110263
721 141459282456260193137317129313
738 144466284461264224139325131317
790 156483290479273277162351153339
852 164518312512286294233375158359
1087 208614364610327343341589284471
1188 229667386663354357356628334581</syntaxhighlight>
 
=={{header|Java}}==
func main() {
<syntaxhighlight lang="java">
primes := rcu.Primes(12000)
import java.math.BigInteger;
sw := ""
import java.util.Arrays;
count := 0
import java.util.List;
i := 0
import java.util.stream.Collectors;
n := new(big.Int)
import java.util.stream.Stream;
fmt.Println("The 4th to the 8th Smarandache-Wellin primes are:")
 
for count < 8 {
public final class SmarandacheWellinPrimes {
sw += strconv.Itoa(primes[i])
 
n.SetString(sw, 10)
public static void main(String[] args) {
if n.ProbablyPrime(15) {
primes = listPrimesUpTo(12_000);
count++
smarandacheWellinPrimes();
if count > 3 {
System.out.println();
fmt.Printf("%dth: index %4d digits %4d last prime %5d\n", count, i+1,
derivedSmarandacheWellinPrimes();
len(sw), primes[i])
}
}
}
private static void smarandacheWellinPrimes() {
i++
int count = 0;
}
int index = 1;
}</syntaxhighlight>
int number = 2;
String numberString = "2";
System.out.println("The first eight Smarandache-Wellin primes are:");
while ( count < 8 ) {
if ( new BigInteger(numberString).isProbablePrime(CERTAINTY_LEVEL) ) {
count += 1;
System.out.println(String.format("%2d%s%-4d%s%-5d%s%d", count, ": index = ", index,
" last prime = ", number, " number of digits = ", numberString.length()));
}
number = primes.get(index);
numberString += number;
index += 1;
}
}
private static void derivedSmarandacheWellinPrimes() {
int count = 0;
int index = 0;
int[] digitFrequencies = new int[10];
System.out.println("The first 20 derived Smarandache-Wellin numbers which are prime:");
while ( count < 20 ) {
String prime = String.valueOf(primes.get(index));
for ( char ch : prime.toCharArray() ) {
digitFrequencies[ch - '0'] += 1;
}
index += 1;
String joined = Arrays.stream(digitFrequencies).mapToObj(String::valueOf).collect(Collectors.joining(""));
if ( new BigInteger(joined).isProbablePrime(CERTAINTY_LEVEL) ) {
count += 1;
System.out.println(String.format("%2d%s%-4d%s%s", count, ": index = ", index, " prime = ", joined));
}
}
}
private static List<Integer> listPrimesUpTo(int limit) {
final int halfLimit = ( limit + 1 ) / 2;
boolean[] composite = new boolean[halfLimit];
for ( int i = 1, p = 3; i < halfLimit; p += 2, i++ ) {
if ( ! composite[i] ) {
for ( int a = i + p; a < halfLimit; a += p ) {
composite[a] = true;
}
}
}
List<Integer> result = Stream.of(2).limit(1).collect(Collectors.toList());
for ( int i = 1, p = 3; i < halfLimit; p += 2, i++ ) {
if ( ! composite[i] ) {
result.add(p);
}
}
return result;
}
private static List<Integer> primes;
private static final int CERTAINTY_LEVEL = 15;
}
</syntaxhighlight>
{{ out }}
<pre>
The first eight Smarandache-Wellin primes are:
1: index = 1 last prime = 2 number of digits = 1
2: index = 2 last prime = 3 number of digits = 2
3: index = 4 last prime = 7 number of digits = 4
4: index = 128 last prime = 719 number of digits = 355
5: index = 174 last prime = 1033 number of digits = 499
6: index = 342 last prime = 2297 number of digits = 1171
7: index = 435 last prime = 3037 number of digits = 1543
8: index = 1429 last prime = 11927 number of digits = 5719
 
The first 20 derived Smarandache-Wellin numbers which are prime:
1: index = 32 prime = 4194123321127
2: index = 72 prime = 547233879626521
3: index = 73 prime = 547233979727521
4: index = 134 prime = 13672766322929571043
5: index = 225 prime = 3916856106393739943689
6: index = 303 prime = 462696313560586013558131
7: index = 309 prime = 532727113760586013758133
8: index = 363 prime = 6430314317473636515467149
9: index = 462 prime = 8734722823685889120488197
10: index = 490 prime = 9035923128899919621189209
11: index = 495 prime = 9036023329699969621389211
12: index = 522 prime = 9337023533410210710923191219
13: index = 538 prime = 94374237357103109113243102223
14: index = 624 prime = 117416265406198131121272110263
15: index = 721 prime = 141459282456260193137317129313
16: index = 738 prime = 144466284461264224139325131317
17: index = 790 prime = 156483290479273277162351153339
18: index = 852 prime = 164518312512286294233375158359
19: index = 1087 prime = 208614364610327343341589284471
20: index = 1188 prime = 229667386663354357356628334581
</pre>
 
=={{header|Julia}}==
{{trans|Go}}
<syntaxhighlight lang="julia">using Primes
using Printf
 
ordi(n) = n == 1 ? "st" : n == 2 ? "nd" : "th"
 
function SmarandacheWellin()
pri = primes(12500)
sw = ""
pcount = 0
i = 1
println("The known Smarandache-Wellin primes are:")
while pcount < 8
sw *= string(pri[i])
if isprime(parse(BigInt, sw))
pcount += 1
le = length(sw)
sws = le > 4 ? sw[1:20] * "..." * sw[le-19:le] : sw
@printf("%d%s: index %4d digits %4d last prime %5d -> %s\n", pcount, ordi(pcount), i, le, pri[i], sws)
end
i += 1
end
println("\nThe first 20 Derived Smarandache-Wellin primes are:")
freqs = zeros(Int, 10)
pcount = 0
i = 1
while pcount < 20
p = string(pri[i])
for d in p
n = parse(Int, string(d)) + 1
freqs[n] += 1
end
dsw = string(parse(BigInt, prod(map(string, freqs))))
if isprime(parse(BigInt, dsw))
pcount += 1
@printf("%4d: index %4d prime %s\n", pcount, i, dsw)
end
i += 1
end
end
 
SmarandacheWellin()
</syntaxhighlight>{{out}} Same as C and Go versions.
 
=={{header|Nim}}==
{{libheader|Nim-Integers}}
<syntaxhighlight lang="Nim">import std/[bitops, math, strformat, strutils, sugar, tables]
 
import integers
 
const N = 12_000
let primes = @[2] & collect(for n in countup(3, N, 2):
if n.isPrime: n)
 
func compressed(str: string; size: int): string =
## Return a compressed value for long strings of digits.
if str.len <= 2 * size: str
else: &"{str[0..<size]}...{str[^size..^1]} ({str.len} digits)"
 
iterator swNumbers(): tuple[value: Integer; lastPrime: int] =
## Yield the SW-Numbers and the last prime added.
var swString = ""
for p in primes:
swString.addInt p
yield (newInteger(swString), p)
 
iterator derivedSwNumbers(): Integer =
## Yield the derived SW-Numbers.
for (n, _) in swNumbers():
var dSwString = ""
let counts = toCountTable($n)
for d in '0'..'9':
dSwString.add $counts[d]
yield newInteger(dSwString.strip(leading = true, trailing = false, {'0'}))
 
echo "Known eight Smarandache-Wellin numbers which are prime:"
var idx, count = 0
for (n, p)in swNumbers():
inc idx
if n.isPrime:
inc count
echo &"{count}: index = {idx:<4} last prime = {p:<5} value = {compressed($n, 20)}"
if count == 8: break
 
echo "\nFirst 20 derived S-W numbers which are prime:"
idx = 0
count = 0
for n in derivedSwNumbers():
inc idx
if n.isPrime:
inc count
echo &"{count:2}: index = {idx:<4} value = {n}"
if count == 20: break
</syntaxhighlight>
 
{{out}}
<pre>Known eight Smarandache-Wellin numbers which are prime:
1: index = 1 last prime = 2 value = 2
2: index = 2 last prime = 3 value = 23
3: index = 4 last prime = 7 value = 2357
4: index = 128 last prime = 719 value = 23571113171923293137...73677683691701709719 (355 digits)
5: index = 174 last prime = 1033 value = 23571113171923293137...10131019102110311033 (499 digits)
6: index = 342 last prime = 2297 value = 23571113171923293137...22732281228722932297 (1171 digits)
7: index = 435 last prime = 3037 value = 23571113171923293137...30013011301930233037 (1543 digits)
8: index = 1429 last prime = 11927 value = 23571113171923293137...11903119091192311927 (5719 digits)
 
First 20 derived S-W numbers which are prime:
1: index = 32 value = 4194123321127
2: index = 72 value = 547233879626521
3: index = 73 value = 547233979727521
4: index = 134 value = 13672766322929571043
5: index = 225 value = 3916856106393739943689
6: index = 303 value = 462696313560586013558131
7: index = 309 value = 532727113760586013758133
8: index = 363 value = 6430314317473636515467149
9: index = 462 value = 8734722823685889120488197
10: index = 490 value = 9035923128899919621189209
11: index = 495 value = 9036023329699969621389211
12: index = 522 value = 9337023533410210710923191219
13: index = 538 value = 94374237357103109113243102223
14: index = 624 value = 117416265406198131121272110263
15: index = 721 value = 141459282456260193137317129313
16: index = 738 value = 144466284461264224139325131317
17: index = 790 value = 156483290479273277162351153339
18: index = 852 value = 164518312512286294233375158359
19: index = 1087 value = 208614364610327343341589284471
20: index = 1188 value = 229667386663354357356628334581
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>
use v5.36;
use ntheory <is_prime next_prime>;
use Lingua::EN::Numbers::Ordinate 'ordinate';
 
sub abbr ($d) { my $l = length $d; $l < 41 ? $d : substr($d,0,20) . '..' . substr($d,-20) . " ($l digits)" }
 
print "Smarandache-Wellen primes:\n";
my($p, $i, $nth, $n) = (0, -1, 0);
do {
$p = next_prime($p);
$n .= $p;
$i++;
(++$nth and printf("%s: Index:%5d Last prime:%6d S-W: %s\n", ordinate $nth, $i, $p, abbr $n)) if is_prime $n;
} until $nth == 8;
 
sub derived ($n) {
my %digits;
$digits{$_}++ for split '', $n;
join '', map { $digits{$_} // 0 } 0..9;
}
 
print "\nSmarandache-Wellen derived primes:\n";
($p, $i, $nth, $n) = (1, -1, 0, '');
do {
$i++;
$n .= ($p = next_prime($p));
++$nth and printf "%4s: Index:%5d %s\n", ordinate $nth, $i, derived $n if is_prime derived $n;
} until $nth == 20;
</syntaxhighlight>
{{out}}
<pre>
The 4th to the 8th Smarandache-WellinWellen primes are:
4th1st: indexIndex: 128 digits0 355Last prime: last prime 7192 S-W: 2
5th2nd: indexIndex: 174 digits1 499Last prime: last prime 1033 3 S-W: 23
6th3rd: indexIndex: 342 digits3 1171 lastLast prime: 2297 7 S-W: 2357
4th: Index: 127 Last prime: 719 S-W: 23571113171923293137..73677683691701709719 (355 digits)
7th: index 435 digits 1543 last prime 3037
5th: Index: 173 Last prime: 1033 S-W: 23571113171923293137..10131019102110311033 (499 digits)
8th: index 1429 digits 5719 last prime 11927
6th: Index: 341 Last prime: 2297 S-W: 23571113171923293137..22732281228722932297 (1171 digits)
7th: Index: 434 Last prime: 3037 S-W: 23571113171923293137..30013011301930233037 (1543 digits)
8th: Index: 1428 Last prime: 11927 S-W: 23571113171923293137..11903119091192311927 (5719 digits)
 
Smarandache-Wellen derived primes:
1st: Index: 31 4194123321127
2nd: Index: 71 547233879626521
3rd: Index: 72 547233979727521
4th: Index: 133 13672766322929571043
5th: Index: 224 3916856106393739943689
6th: Index: 302 462696313560586013558131
7th: Index: 308 532727113760586013758133
8th: Index: 362 6430314317473636515467149
9th: Index: 461 8734722823685889120488197
10th: Index: 489 9035923128899919621189209
11th: Index: 494 9036023329699969621389211
12th: Index: 521 9337023533410210710923191219
13th: Index: 537 94374237357103109113243102223
14th: Index: 623 117416265406198131121272110263
15th: Index: 720 141459282456260193137317129313
16th: Index: 737 144466284461264224139325131317
17th: Index: 789 156483290479273277162351153339
18th: Index: 851 164518312512286294233375158359
19th: Index: 1086 208614364610327343341589284471
20th: Index: 1187 229667386663354357356628334581
</pre>
 
Line 340 ⟶ 896:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Smarandache-WhellenWellin primes:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span> <span style="color: #008080;">in</span> <span style="color: #000000;">swp</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %d%s:"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Index: %4d, Last prime %5d, %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Smarandache-WhellenWellin derived primes:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span> <span style="color: #008080;">in</span> <span style="color: #000000;">swdp</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %d%s:"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)})</span>
Line 354 ⟶ 910:
{{out}}
<pre>
Smarandache-WhellenWellin primes:
1st: Index: 1, Last prime 2, 2
2nd: Index: 2, Last prime 3, 23
Line 363 ⟶ 919:
7th: Index: 435, Last prime 3037, 23571113171923293137...30013011301930233037 (1,543 digits)
8th: Index: 1429, Last prime 11927, 23571113171923293137...11903119091192311927 (5,719 digits)
Smarandache-WhellenWellin derived primes:
1st: Index: 32, 4194123321127
2nd: Index: 72, 547233879626521
Line 393 ⟶ 949:
my @primes = (^∞).grep: &is-prime;
 
my @Smarandache-WhellenWellin = [\~] @primes;
 
sink @Smarandache-WhellenWellin[1500]; # pre-reify for concurrency
 
sub derived ($n) { my %digits = $n.comb.Bag; (0..9).map({ %digits{$_} // 0 }).join }
Line 401 ⟶ 957:
sub abbr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '…' ~ .substr(*-20) ~ " ({.chars} digits)" }
 
say "Smarandache-WhellenWellin primes:";
say ordinal-digit(++$,:u).fmt("%4s") ~ $_ for (^∞).hyper(:4batch).map({
next unless (my $sw = @Smarandache-WhellenWellin[$_]).is-prime;
sprintf ": Index: %4d, Last prime: %5d, %s", $_, @primes[$_], $sw.&abbr
})[^8];
 
say "\nSmarandache-WhellenWellin derived primes:";
say ordinal-digit(++$,:u).fmt("%4s") ~ $_ for (^∞).hyper(:8batch).map({
next unless (my $sw = @Smarandache-WhellenWellin[$_].&derived).is-prime;
sprintf ": Index: %4d, %s", $_, $sw
})[^20];</syntaxhighlight>
{{out}}
<pre>Smarandache-WhellenWellin primes:
1ˢᵗ: Index: 0, Last prime: 2, 2
2ⁿᵈ: Index: 1, Last prime: 3, 23
Line 423 ⟶ 979:
8ᵗʰ: Index: 1428, Last prime: 11927, 23571113171923293137…11903119091192311927 (5719 digits)
 
Smarandache-WhellenWellin derived primes:
1ˢᵗ: Index: 31, 4194123321127
2ⁿᵈ: Index: 71, 547233879626521
Line 444 ⟶ 1,000:
19ᵗʰ: Index: 1086, 208614364610327343341589284471
20ᵗʰ: Index: 1187, 229667386663354357356628334581</pre>
 
=={{header|RPL}}==
The latest versions of RPL can handle large 500-digit integers, making it possible to search for the fifth SW prime. Unfortunately, even with an emulator, the primality test takes too long.
{{works with|HP|49}}
« "" 2
1 4 ROLL '''FOR''' j
SWAP OVER + SWAP NEXTPRIME
'''NEXT''' DROP
» » '<span style="color:blue">→SW</span>' STO
« →STR → swn
« { 10 } 0 CON
1 swn SIZE '''FOR''' j
swn j DUP SUB STR→ 1 + DUP2 GET 1 + PUT
'''NEXT'''
""
1 10 '''FOR''' j
OVER j GET +
'''NEXT'''
STR→ NIP
» '<span style="color:blue">DSW</span>' STO
« 0 → idx
« { }
'''WHILE''' DUP SIZE 4 < '''REPEAT'''
'''IF''' 'idx' INCR <span style="color:blue">→SW</span> DUP ISPRIME? '''THEN''' + '''ELSE''' DROP '''END'''
'''END'''
» » '<span style="color:blue">TASK1</span>' STO
« 0 → idx
« { }
'''WHILE''' DUP SIZE 4 < '''REPEAT'''
'''IF''' 'idx' INCR <span style="color:blue">→SW DSW</span> DUP ISPRIME? '''THEN''' + '''ELSE''' DROP '''END'''
'''END'''
» » '<span style="color:blue">TASK2</span>' STO
{{out}}
<pre>
2: { 2 23 2357 2357111317192329313741434753596167717379838997101103107109113127131137139149151157163167173179181191193197199211223227229233239241251257263269271277281283293307311313317331337347349353359367373379383389397401409419421431433439443449457461463467479487491499503509521523541547557563569571577587593599601607613617619631641643647653659661673677683691701709719 }
1: { 4194123321127 547233879626521 547233979727521 13672766322929571043 }
</pre>
 
=={{header|Ruby}}==
The first seven Smarandache-Wellin primes are found in about 12 seconds on my system. The eighth (not shown here) takes nine minutes.
<syntaxhighlight lang="ruby" line>require 'prime'
require 'openssl' # for it's faster 'prime?' method
 
Smarandache_Wellins = Struct.new(:index, :last_prime, :sw, :derived)
 
def derived(n)
counter = Array.new(10){|i| [i, 0]}.to_h # counter is a Hash
n.digits.tally(counter).values.join.to_i
end
 
def prime?(n) = OpenSSL::BN.new(n).prime?
 
ord = {1 => "1st", 2 => "2nd", 3 => "3rd"}
ord.default_proc = proc {|_h, k| k.to_s + "th"} # _ in _h means "not used"
 
smarandache_wellinses = Enumerator.new do |y|
str = ""
Prime.each.with_index(1) do |pr, i|
str += pr.to_s
sw = str.to_i
y << Smarandache_Wellins.new(i, pr, sw, derived(sw))
end
end
 
smarandache_wellins_primes = Enumerator.new do |y|
smarandache_wellinses.rewind
loop do
s_w = smarandache_wellinses.next
y << s_w if prime?(s_w.sw)
end
end
 
sw_derived_primes = Enumerator.new do |y|
smarandache_wellinses.rewind
loop do
sw = smarandache_wellinses.next
y << sw if prime?(sw.derived)
end
end
 
n = 3
puts "The first #{n} Smarandache-Wellin primes are:"
puts smarandache_wellins_primes.first(n).map(&:sw)
puts "\nThe first #{n} Smarandache-Wellin derived primes are:"
puts sw_derived_primes.first(n).map(&:derived)
 
n = 7
puts "\nThe first #{n} Smarandache-Wellin primes are:"
smarandache_wellins_primes.first(n).each.with_index(1) do |swp, i|
puts "%s: index %4d, digits %4d, last prime %5d " % [ord[i], swp.index, swp.sw.digits.size, swp.last_prime]
end
 
n = 20
puts "\nThe first #{n} Derived Smarandache-Wellin primes are:"
sw_derived_primes.first(n).each.with_index(1) do |swdp, i|
puts "%4s: index %4d, prime %s" % [ord[i], swdp.index, swdp.derived]
end</syntaxhighlight>
{{out}}
<pre>The first 3 Smarandache-Wellin primes are:
2
23
2357
 
The first 3 Smarandache-Wellin derived primes are:
4194123321127
547233879626521
547233979727521
 
The first 7 Smarandache-Wellin primes are:
1st: index 1, digits 1, last prime 2
2nd: index 2, digits 2, last prime 3
3rd: index 4, digits 4, last prime 7
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
 
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>
 
=={{header|Wren}}==
Line 450 ⟶ 1,148:
{{libheader|Wren-gmp}}
Need to use GMP here to find the 8th S-W prime in a reasonable time (35.5 seconds on my Core i7 machine).
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./gmp" for Mpz
import "./fmt"for Fmt
9,486

edits