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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

A magnanimous number is an integer where there is no place in the number where a + (plus sign) could be added between any two digits to give a non-prime sum.

Traditionally the single digit numbers 0 through 9 are included as magnanimous numbers as there is no place in the number where you can add a plus between two digits at all. (Kind of weaselly but there you are...) Except for the actual value 0, leading zeros are not permitted. Internal zeros are fine though, 1001 -> 1 + 001 (prime), 10 + 01 (prime) 100 + 1 (prime). There are only 571 known magnanimous numbers. It is strongly suspected, though not rigorously proved, that there are no magnanimous numbers above 97393713331910, the largest one known.

Let's start with the solution:

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

Source code in the fsharp programming language

// Generate Magnanimous numbers. Nigel Galloway: March 20th., 2020
let rec fN n g = match (g/n,g%n) with
                  (0,_)                    -> true
                 |(α,β) when isPrime (α+β) -> fN (n*10) g
                 |_                        -> false
let Magnanimous = let Magnanimous = fN 10 in seq{yield! {0..9}; yield! Seq.initInfinite id |> Seq.skip 10 |> Seq.filter Magnanimous}


Magnanimous |> Seq.take 45 |> Seq.iter (printf "%d "); printfn ""


Magnanimous |> Seq.skip 240 |> Seq.take 10 |> Seq.iter (printf "%d "); printfn "";;


Magnanimous |> Seq.skip 390 |> Seq.take 10 |> Seq.iter (printf "%d "); printfn "";;


  

You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Arendelle programming language
You may also check:How to resolve the algorithm Hough transform step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the F# programming language
You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Merge and aggregate datasets step by step in the Java programming language