How to resolve the algorithm Rare numbers step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rare numbers step by step in the F# programming language

Table of Contents

Problem Statement

Rare   numbers are positive integers   n   where:

Show all output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rare numbers step by step in the F# programming language

Source code in the fsharp programming language

// Find all Rare numbers with a digits. Nigel Galloway: September 18th., 2019. 
let rareNums a=
  let tN=set[1L;4L;5L;6L;9L]
  let izPS g=let n=(float>>sqrt>>int64)g in n*n=g
  let n=[for n in [0..a/2-1] do yield ((pown 10L (a-n-1))-(pown 10L n))]|>List.rev
  let rec fN i g e=seq{match e with 0->yield g |e->for n in i do yield! fN [-9L..9L] (n::g) (e-1)}|>Seq.filter(fun g->let g=Seq.map2(*) n g|>Seq.sum in g>0L && izPS g)
  let rec fG n i g e l=seq{
    match l with
     h::t->for l in max 0L (0L-h)..min 9L (9L-h) do if e>1L||l=0L||tN.Contains((2L*l+h)%10L) then yield! fG (n+l*e+(l+h)*g) (i+l*g+(l+h)*e) (g/10L) (e*10L) t
    |_->if n>(pown 10L (a-1)) then for l in (if a%2=0 then [0L] else [0L..9L]) do let g=l*(pown 10L (a/2)) in if izPS (n+i+2L*g) then yield (i+g,n+g)} 
  fN [0L..9L] [] (a/2) |> Seq.collect(List.rev >> fG 0L 0L (pown 10L (a-1)) 1L)


let test n=
  let t = System.Diagnostics.Stopwatch.StartNew()
  for n in (rareNums n) do printfn "%A" n
  t.Stop()
  printfn "Elapsed Time: %d ms for length %d" t.ElapsedMilliseconds n

[2..17] |> Seq.iter test


  

You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Knapsack problem/Bounded step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Filter step by step in the PHP programming language
You may also check:How to resolve the algorithm String length step by step in the Run BASIC programming language