How to resolve the algorithm Smarandache-Wellin primes step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Smarandache-Wellin primes step by step in the Perl programming language

Table of Contents

Problem Statement

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. '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. 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Smarandache-Wellin primes step by step in the Perl programming language

Source code in the perl programming language

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;


  

You may also check:How to resolve the algorithm Least common multiple step by step in the Ursa programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Sidef programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the EchoLisp programming language