Smarandache-Wellin primes
You are encouraged to solve this task according to the task description, using any language you may know.
- 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.
A Derived S-W number (not an 'official' term) is an integer formed from a S-W number by working out the number of times each of the digits 0 to 9 occurs in that number, concatenating those frequencies in the same order (i.e. frequency of '0' first, frequency of '1' second etc) and removing any leading zeros.
- Examples
'23571113' is the sixth S-W number formed by concatenating the first 6 primes: 2, 3, 5, 7, 11 and 13.
The corresponding Derived S-W number is '312010100' because '1' occurs 3 times, '3' occurs twice and '2', '5' and '7' all occur once.
- Task
- Find and show the first three S-W numbers which are prime.
- Find and show the first three Derived S-W numbers which are prime.
- Stretch (requires 'big integer' support)
Find and show the index in the sequence (starting from 1), the total number of digits and the last prime used to form the fourth, fifth, sixth, seventh and (optionally) the eighth S-W numbers which are prime or probably prime with reasonable certainty. It is unknown whether there are any more but, if you fancy searching for one, good luck! You can start from an index of 22,077.
Also find and show up to the first twenty Derived S-W numbers which are prime and their index in the sequence.
- References
- Wikipedia: Smarandache-Wellin number
- OEIS:A019518 - Smarandache-Wellin numbers
- OEIS:A069151 - Smarandache-Wellin primes
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;
}
const char *ord(int count) {
return (count == 1) ? "st" :
(count == 2) ? "nd" :
(count == 3) ? "rd" : "th";
}
int main() {
bool *c = sieve(12000);
char sw[6000] = "";
char tmp[20];
int count = 0, p = 1, ix = 0, i;
mpz_t n;
mpz_init(n);
printf("The known 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++;
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;
ix = 0;
while (count < 20) {
while (c[++p]);
sprintf(tmp, "%d", p);
for (i = 0; i < strlen(tmp); ++i) freqs[tmp[i]-48]++;
char dsw[40] = "";
for (i = 0; i < 10; ++i) {
sprintf(tmp, "%d", freqs[i]);
strcat(dsw, tmp);
}
int idx = -1;
for (i = 0; i < strlen(dsw); ++i) {
if (dsw[i] != '0') {
idx = i;
break;
}
}
mpz_set_str(n, dsw + idx, 10);
if (mpz_probab_prime_p(n, 15) > 0) {
count++;
printf("%2d%s: index %4d prime %s\n", count, ord(count), ix+1, dsw + idx);
}
ix++;
}
free(c);
return 0;
}
- Output:
The known Smarandache-Wellin primes are: 1st: index 1 digits 1 last prime 2 -> 2 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 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
C++
#include <iomanip>
#include <iostream>
#include <string>
#include <primesieve.hpp>
#include <gmpxx.h>
using big_int = mpz_class;
class sw_number_generator {
public:
sw_number_generator() { next(); }
void next();
const std::string& number() const { return number_; }
uint64_t prime() const { return prime_; }
int index() const { return index_; }
std::string derived_number() const;
private:
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;
}
}
}
void print_derived_sw_primes(int count) {
std::cout << "First " << count << " Derived Smarandache-Wellin primes:\n\n"
<< " |Index|Derived Smarandache-Wellin prime\n"
<< "-----------------------------------------\n";
sw_number_generator swg;
for (int n = 1; n <= count; swg.next()) {
std::string num = swg.derived_number();
if (is_probably_prime(big_int(num))) {
std::cout << std::setw(2) << n << "|"
<< std::setw(5) << swg.index() << "|"
<< std::setw(32) << num << '\n';
++n;
}
}
}
int main() {
print_sw_primes();
std::cout << '\n';
print_derived_sw_primes(20);
}
- Output:
Known Smarandache-Wellin primes: |Index|Digits|Last prime| Smarandache-Wellin prime ---------------------------------------------------------------------- 1| 1| 1| 2| 2 2| 2| 2| 3| 23 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
EasyLang
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[]
- Output:
[ 2 23 2357 ] [ 4194123321127 547233879626521 547233979727521 ]
F#
// 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}")
- Output:
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
Go
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.
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(12000)
sw := ""
count := 0
i := 0
n := new(big.Int)
fmt.Println("The known Smarandache-Wellin primes are:")
for count < 8 {
sw += strconv.Itoa(primes[i])
n.SetString(sw, 10)
if n.ProbablyPrime(15) {
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("\nThe first 20 Derived Smarandache-Wellin primes are:")
freqs := make([]int, 10)
count = 0
i = 0
for count < 20 {
p := strconv.Itoa(primes[i])
for _, d := range p {
n, _ := strconv.Atoi(string(d))
freqs[n]++
}
dsw := ""
for _, freq := range freqs {
dsw += strconv.Itoa(freq)
}
dsw = strings.TrimLeft(dsw, "0")
n.SetString(dsw, 10)
if n.ProbablyPrime(15) {
count++
fmt.Printf("%2d%s: index %4d prime %v\n", count, ord(count), i+1, n)
}
i++
}
}
- Output:
The known Smarandache-Wellin primes are: 1st: index 1 digits 1 last prime 2 -> 2 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 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
J
derive=. [: <:@#/.~ i.@10 , ]
digits=. ;@:(<@("."0@":)"0)
(#~ 1&p:) 10&#.@digits\ p: i. 10
2 23 2357
(#~ 1 p: {:"1) (# , 10x&#.@digits@derive@digits)\ p: i. 1200
32 4194123321127
72 547233879626521
73 547233979727521
134 13672766322929571043
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
Java
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class SmarandacheWellinPrimes {
public static void main(String[] args) {
primes = listPrimesUpTo(12_000);
smarandacheWellinPrimes();
System.out.println();
derivedSmarandacheWellinPrimes();
}
private static void smarandacheWellinPrimes() {
int count = 0;
int index = 1;
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;
}
- Output:
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
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()
- Output:
Same as C and Go versions.
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
- Output:
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
Perl
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;
- Output:
Smarandache-Wellen primes: 1st: Index: 0 Last prime: 2 S-W: 2 2nd: Index: 1 Last prime: 3 S-W: 23 3rd: Index: 3 Last prime: 7 S-W: 2357 4th: Index: 127 Last prime: 719 S-W: 23571113171923293137..73677683691701709719 (355 digits) 5th: Index: 173 Last prime: 1033 S-W: 23571113171923293137..10131019102110311033 (499 digits) 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
Phix
Rather slow...
with javascript_semantics include mpfr.e string sw = "" mpz n = mpz_init(), dn = mpz_init() sequence swp = {}, swdp = {}, dc = repeat(0,10) integer np = 0 atom t0 = time(), t1 = time()+1 while length(swp)<iff(platform()=JS?5:8) do np += 1 integer p = get_prime(np) string sp = sprintf("%d",p) sw &= sp mpz_set_str(n,sw,10) if mpz_prime(n) then swp = append(swp,{np,p,shorten(sw)}) end if for c in sp do dc[c-'0'+1] += 1 end for integer dc9 = dc[10] if odd(dc9) and remainder(dc9,5)!=0 then string swdps = join(dc,"",fmt:="%d") mpz_set_str(dn,swdps,10) if mpz_prime(dn) then swdp = append(swdp,{np,swdps}) end if end if if platform()!=JS and time()>t1 then progress("processing prime %d...\r",{np}) t1 = time()+1 end if end while printf(1,"Smarandache-Wellin primes:\n") for i,s in swp do printf(1," %d%s:",{i,ord(i)}) printf(1," Index: %4d, Last prime %5d, %s\n",s) end for printf(1,"Smarandache-Wellin derived primes:\n") for i,s in swdp do printf(1," %d%s:",{i,ord(i)}) printf(1," Index: %4d, %s\n",s) end for ?elapsed(time()-t0)
- Output:
Smarandache-Wellin primes: 1st: Index: 1, Last prime 2, 2 2nd: Index: 2, Last prime 3, 23 3rd: Index: 4, Last prime 7, 2357 4th: Index: 128, Last prime 719, 23571113171923293137...73677683691701709719 (355 digits) 5th: Index: 174, Last prime 1033, 23571113171923293137...10131019102110311033 (499 digits) 6th: Index: 342, Last prime 2297, 23571113171923293137...22732281228722932297 (1,171 digits) 7th: Index: 435, Last prime 3037, 23571113171923293137...30013011301930233037 (1,543 digits) 8th: Index: 1429, Last prime 11927, 23571113171923293137...11903119091192311927 (5,719 digits) Smarandache-Wellin derived primes: 1st: Index: 32, 4194123321127 2nd: Index: 72, 547233879626521 3rd: Index: 73, 547233979727521 4th: Index: 134, 13672766322929571043 5th: Index: 225, 3916856106393739943689 6th: Index: 303, 462696313560586013558131 7th: Index: 309, 532727113760586013758133 8th: Index: 363, 6430314317473636515467149 9th: Index: 462, 8734722823685889120488197 10th: Index: 490, 9035923128899919621189209 11th: Index: 495, 9036023329699969621389211 12th: Index: 522, 9337023533410210710923191219 13th: Index: 538, 94374237357103109113243102223 14th: Index: 624, 117416265406198131121272110263 15th: Index: 721, 141459282456260193137317129313 16th: Index: 738, 144466284461264224139325131317 17th: Index: 790, 156483290479273277162351153339 18th: Index: 852, 164518312512286294233375158359 19th: Index: 1087, 208614364610327343341589284471 20th: Index: 1188, 229667386663354357356628334581 "13 minutes and 13s"
Raku
The first seven Smarandache-Wellin primes are found in a few seconds on my system. The eighth adds over five minutes to the run time.
use Lingua::EN::Numbers;
my @primes = (^∞).grep: &is-prime;
my @Smarandache-Wellin = [\~] @primes;
sink @Smarandache-Wellin[1500]; # pre-reify for concurrency
sub derived ($n) { my %digits = $n.comb.Bag; (0..9).map({ %digits{$_} // 0 }).join }
sub abbr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '…' ~ .substr(*-20) ~ " ({.chars} digits)" }
say "Smarandache-Wellin primes:";
say ordinal-digit(++$,:u).fmt("%4s") ~ $_ for (^∞).hyper(:4batch).map({
next unless (my $sw = @Smarandache-Wellin[$_]).is-prime;
sprintf ": Index: %4d, Last prime: %5d, %s", $_, @primes[$_], $sw.&abbr
})[^8];
say "\nSmarandache-Wellin derived primes:";
say ordinal-digit(++$,:u).fmt("%4s") ~ $_ for (^∞).hyper(:8batch).map({
next unless (my $sw = @Smarandache-Wellin[$_].&derived).is-prime;
sprintf ": Index: %4d, %s", $_, $sw
})[^20];
- Output:
Smarandache-Wellin primes: 1ˢᵗ: Index: 0, Last prime: 2, 2 2ⁿᵈ: Index: 1, Last prime: 3, 23 3ʳᵈ: Index: 3, Last prime: 7, 2357 4ᵗʰ: Index: 127, Last prime: 719, 23571113171923293137…73677683691701709719 (355 digits) 5ᵗʰ: Index: 173, Last prime: 1033, 23571113171923293137…10131019102110311033 (499 digits) 6ᵗʰ: Index: 341, Last prime: 2297, 23571113171923293137…22732281228722932297 (1171 digits) 7ᵗʰ: Index: 434, Last prime: 3037, 23571113171923293137…30013011301930233037 (1543 digits) 8ᵗʰ: Index: 1428, Last prime: 11927, 23571113171923293137…11903119091192311927 (5719 digits) Smarandache-Wellin derived primes: 1ˢᵗ: Index: 31, 4194123321127 2ⁿᵈ: Index: 71, 547233879626521 3ʳᵈ: Index: 72, 547233979727521 4ᵗʰ: Index: 133, 13672766322929571043 5ᵗʰ: Index: 224, 3916856106393739943689 6ᵗʰ: Index: 302, 462696313560586013558131 7ᵗʰ: Index: 308, 532727113760586013758133 8ᵗʰ: Index: 362, 6430314317473636515467149 9ᵗʰ: Index: 461, 8734722823685889120488197 10ᵗʰ: Index: 489, 9035923128899919621189209 11ᵗʰ: Index: 494, 9036023329699969621389211 12ᵗʰ: Index: 521, 9337023533410210710923191219 13ᵗʰ: Index: 537, 94374237357103109113243102223 14ᵗʰ: Index: 623, 117416265406198131121272110263 15ᵗʰ: Index: 720, 141459282456260193137317129313 16ᵗʰ: Index: 737, 144466284461264224139325131317 17ᵗʰ: Index: 789, 156483290479273277162351153339 18ᵗʰ: Index: 851, 164518312512286294233375158359 19ᵗʰ: Index: 1086, 208614364610327343341589284471 20ᵗʰ: Index: 1187, 229667386663354357356628334581
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.
« "" 2 1 4 ROLL FOR j SWAP OVER + SWAP NEXTPRIME NEXT DROP » » '→SW' 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 » 'DSW' STO « 0 → idx « { } WHILE DUP SIZE 4 < REPEAT IF 'idx' INCR →SW DUP ISPRIME? THEN + ELSE DROP END END » » 'TASK1' STO « 0 → idx « { } WHILE DUP SIZE 4 < REPEAT IF 'idx' INCR →SW DSW DUP ISPRIME? THEN + ELSE DROP END END » » 'TASK2' STO
- Output:
2: { 2 23 2357 2357111317192329313741434753596167717379838997101103107109113127131137139149151157163167173179181191193197199211223227229233239241251257263269271277281283293307311313317331337347349353359367373379383389397401409419421431433439443449457461463467479487491499503509521523541547557563569571577587593599601607613617619631641643647653659661673677683691701709719 } 1: { 4194123321127 547233879626521 547233979727521 13672766322929571043 }
Ruby
The first seven Smarandache-Wellin primes are found in about 12 seconds on my system. The eighth (not shown here) takes nine minutes.
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
- Output:
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
Sidef
say "Smarandache-Wellen primes:"
for (var (p=2, i=1, n='', nth=1); nth <= 8; (p.next_prime!, ++i)) {
n += Str(p)
if (Num(n).is_prime){
var t = n
if (t.len > 50) {
t = (t.first(20) + ' ... ' + t.last(20) + " (#{t.len} digits)")
}
printf("%s: Index:%5d Last prime:%6d S-W: %s\n", nth, i, p, t)
++nth
}
}
say "\nSmarandache-Wellen derived primes:"
func derived(String n) -> String {
var freq = n.chars.freq
0..9 -> map { freq{_} \\ 0 }.join
}
for (var (p=2, i=1, nth=1, n=''); nth <= 20; (p.next_prime!, ++i)) {
n += Str(p)
var t = derived(n)
if (Num(t).is_prime){
if (t.len > 50) {
t = (t.first(20) + ' ... ' + t.last(20) + " (#{t.len} digits)")
}
printf("%2s: Index:%5d %s\n", nth, i, t)
++nth
}
}
- Output:
Smarandache-Wellen primes: 1: Index: 1 Last prime: 2 S-W: 2 2: Index: 2 Last prime: 3 S-W: 23 3: Index: 4 Last prime: 7 S-W: 2357 4: Index: 128 Last prime: 719 S-W: 23571113171923293137 ... 73677683691701709719 (355 digits) 5: Index: 174 Last prime: 1033 S-W: 23571113171923293137 ... 10131019102110311033 (499 digits) 6: Index: 342 Last prime: 2297 S-W: 23571113171923293137 ... 22732281228722932297 (1171 digits) 7: Index: 435 Last prime: 3037 S-W: 23571113171923293137 ... 30013011301930233037 (1543 digits) 8: Index: 1429 Last prime: 11927 S-W: 23571113171923293137 ... 11903119091192311927 (5719 digits) Smarandache-Wellen derived primes: 1: Index: 32 4194123321127 2: Index: 72 547233879626521 3: Index: 73 547233979727521 4: Index: 134 13672766322929571043 5: Index: 225 3916856106393739943689 6: Index: 303 462696313560586013558131 7: Index: 309 532727113760586013758133 8: Index: 363 6430314317473636515467149 9: Index: 462 8734722823685889120488197 10: Index: 490 9035923128899919621189209 11: Index: 495 9036023329699969621389211 12: Index: 522 9337023533410210710923191219 13: Index: 538 94374237357103109113243102223 14: Index: 624 117416265406198131121272110263 15: Index: 721 141459282456260193137317129313 16: Index: 738 144466284461264224139325131317 17: Index: 790 156483290479273277162351153339 18: Index: 852 164518312512286294233375158359 19: Index: 1087 208614364610327343341589284471 20: Index: 1188 229667386663354357356628334581
Wren
Need to use GMP here to find the 8th S-W prime in a reasonable time (35.5 seconds on my Core i7 machine).
import "./math" for Int
import "./gmp" for Mpz
import "./fmt"for Fmt
var primes = Int.primeSieve(12000)
var sw = ""
var count = 0
var i = 0
var n = Mpz.new()
System.print("The known Smarandache-Wellin primes are:")
while (count < 8) {
sw = sw + primes[i].toString
n.setStr(sw)
if (n.probPrime(15) > 0) {
count = count + 1
Fmt.print("$r: index $4d digits $4d last prime $5d -> $20a", count, i+1, sw.count, primes[i], n)
}
i = i + 1
}
System.print("\nThe first 20 Derived Smarandache-Wellin primes are:")
var freqs = List.filled(10, 0)
count = 0
i = 0
while (count < 20) {
var p = primes[i].toString
for (d in p) {
var n = Num.fromString(d)
freqs[n] = freqs[n] + 1
}
var dsw = freqs.join("").trimStart("0")
n.setStr(dsw)
if (n.probPrime(15) > 0) {
count = count + 1
Fmt.print("$4r: index $4d prime $i", count, i+1, n)
}
i = i + 1
}
- Output:
The known Smarandache-Wellin primes are: 1st: index 1 digits 1 last prime 2 -> 2 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 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