Smarandache-Wellin primes

Revision as of 13:06, 25 March 2023 by Thundergnat (talk | contribs) (→‎{{header|Raku}}: Add a Raku example)

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.

Smarandache-Wellin primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definitions

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.

References


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.

my @primes = (^∞).grep: &is-prime;

my @Smarandache-Whellen = [\~] @primes;

sink @Smarandache-Whellen[1500]; # pre-reify for concurrency

sub derived ($n) { my %digits = $n.comb.Bag; (1..9).map({ %digits{$_} // 0 }).join }

sub abbr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '…' ~ .substr(*-20) ~ " ({.chars} digits)" }

say "Smarandache-Whellen primes:\n " ~
(^∞).hyper(:4batch).map({
    next unless (my $sw = @Smarandache-Whellen[$_]).is-prime;
    sprintf " Index: %4d, Last prime: %5d, %s\n", $_, @primes[$_], $sw.&abbr
})[^8];

say "\nSmarandache-Whellen derived primes:\n " ~
(^∞).hyper(:8batch).map({
    next unless (my $sw = @Smarandache-Whellen[$_].&derived).is-prime;
    sprintf " Index: %4d, %s\n", $_, $sw
})[^10];
Output:
Smarandache-Whellen primes:
  Index:    0, Last prime:     2, 2
  Index:    1, Last prime:     3, 23
  Index:    3, Last prime:     7, 2357
  Index:  127, Last prime:   719, 23571113171923293137…73677683691701709719 (355 digits)
  Index:  173, Last prime:  1033, 23571113171923293137…10131019102110311033 (499 digits)
  Index:  341, Last prime:  2297, 23571113171923293137…22732281228722932297 (1171 digits)
  Index:  434, Last prime:  3037, 23571113171923293137…30013011301930233037 (1543 digits)
  Index: 1428, Last prime: 11927, 23571113171923293137…11903119091192311927 (5719 digits)


Smarandache-Whellen derived primes:
  Index:   64, 45232857623519
  Index:   73, 47234179728521
  Index:  101, 55265428181036833
  Index:  108, 56265628251240937
  Index:  110, 57265628251441937
  Index:  112, 59265728251642937
  Index:  122, 63266131272746939
  Index:  153, 723172323232702949
  Index:  208, 1465092363737883583
  Index:  230, 17557110463939953691


Wren

Basic

Library: Wren-math
Library: Wren-fmt
import "./math" for Int
import "./fmt" for Fmt

var primes = Int.primeSieve(400)
var sw = ""
var swp = []
var count = 0
var i = 0
while (count < 3) {
    sw = sw + primes[i].toString
    var n = Num.fromString(sw)
    if (Int.isPrime(n)) {
        swp.add(n)
        count = count + 1
    }
    i = i + 1
}
System.print("The first 3 Smarandache-Wellin primes are:")
Fmt.print("$d", swp)

var freqs = List.filled(10, 0)
var dswp = []
count = 0
i = 0
while (count < 3) {
    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")
    var dn = Num.fromString(dsw)
    if (Int.isPrime(dn)) {
        dswp.add(dn)
        count = count + 1
    }
    i = i + 1
}
System.print("\nThe first 3 Derived Smarandache-Wellin primes are:")
Fmt.print("$d", dswp)
Output:
The first 3 Smarandache-Wellin primes are:
2 23 2357

The first 3 Derived Smarandache-Wellin primes are:
4194123321127 547233879626521 547233979727521

Stretch

Library: 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).

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 4th to the 8th Smarandache-Wellin primes are:")
while (count < 8) {
    sw = sw + primes[i].toString
    n.setStr(sw)
    if (n.probPrime(15) > 0) {
        count = count + 1
        if (count > 3) {
            Fmt.print("$r: index $4d  digits $4d  last prime $5d", count, i+1, sw.count, primes[i])
        }
    }
    i = i + 1
}
Output:
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