How to resolve the algorithm Smarandache-Wellin primes step by step in the Raku programming language
How to resolve the algorithm Smarandache-Wellin primes step by step in the Raku 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 Raku programming language
Source code in the raku programming language
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];
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the C# programming language
You may also check:How to resolve the algorithm Text processing/1 step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the Scheme programming language
You may also check:How to resolve the algorithm Enumerations step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Mathematica / Wolfram Language programming language