How to resolve the algorithm Four is magic step by step in the F# programming language
How to resolve the algorithm Four is magic step by step in the F# programming language
Table of Contents
Problem Statement
Write a subroutine, function, whatever it may be called in your language, that takes an integer number and returns an English text sequence starting with the English cardinal representation of that integer, the word 'is' and then the English cardinal representation of the count of characters that made up the first word, followed by a comma. Continue the sequence by using the previous count word as the first word of the next phrase, append 'is' and the cardinal count of the letters in that word. Continue until you reach four. Since four has four characters, finish by adding the words 'four is magic' and a period. All integers will eventually wind up at four. For instance, suppose your are given the integer 3. Convert 3 to Three, add is , then the cardinal character count of three, or five, with a comma to separate if from the next phrase. Continue the sequence five is four, (five has four letters), and finally, four is magic. For reference, here are outputs for 0 through 9.
You can choose to use a library, (module, external routine, whatever) to do the cardinal conversions as long as the code is easily and freely available to the public.
If you roll your own, make the routine accept at minimum any integer from 0 up to 999999. If you use a pre-made library, support at least up to unsigned 64 bit integers. (or the largest integer supported in your language if it is less.)
Four is magic is a popular code-golf task. This is not code golf. Write legible, idiomatic and well formatted code.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Four is magic step by step in the F# programming language
Source code in the fsharp programming language
//Express an Integer in English Language. Nigel Galloway: September 19th., 2018
let fN=[|[|"";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"|];
[|"ten";"eleven";"twelve";"thirteen";"fourteen";"fifteen";"sixteen";"seventeen";"eighteen";"nineteen"|];
[|"";"";"twenty";"thirty";"fourty";"fifty";"sixty";"seventy";"eighty";"ninety"|]|]
let rec I2α α β=match α with |α when α<20 ->β+fN.[α/10].[α%10]
|α when α<100 ->I2α (α%10) (β+fN.[2].[α/10]+if α%10>0 then " " else "")
|α when α<1000 ->I2α (α-(α/100)*100) (β+fN.[0].[α/100]+" hunred"+if α%100>0 then " and " else "")
|α when α<1000000->I2α (α%1000) (β+(I2α (α/1000) "")+" thousand"+if α%100=0 then "" else if (α-(α/1000)*1000)<100 then " and " else " ")
let rec printI2α=function |0->printf "naught->"; printI2α 6
|4->printfn "four is magic"
|n when n<0->let g = I2α -n "minus " in printf "%s->" g; printI2α (g.Length)
|n ->let g = I2α n "" in printf "%s->" g; printI2α (g.Length)
let N=System.Random()
List.init 25 (fun _->N.Next 999999) |> List.iter printI2α
You may also check:How to resolve the algorithm Associative array/Creation step by step in the XLISP programming language
You may also check:How to resolve the algorithm Twelve statements step by step in the Perl programming language
You may also check:How to resolve the algorithm Factorial base numbers indexing permutations of a collection step by step in the Python programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Nim programming language
You may also check:How to resolve the algorithm Zebra puzzle step by step in the Bracmat programming language