How to resolve the algorithm Smarandache-Wellin primes step by step in the F# programming language
How to resolve the algorithm Smarandache-Wellin primes step by step in the F# 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 F# programming language
Source code in the fsharp programming language
// 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}")
You may also check:How to resolve the algorithm Sort an integer array step by step in the Tcl programming language
You may also check:How to resolve the algorithm Quine step by step in the INTERCAL programming language
You may also check:How to resolve the algorithm Bitmap/Read a PPM file step by step in the Nim programming language
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Wren programming language