/* Spelling numbers (English) in Picat. From my Euler #17 program: http://www.hakank.org/picat/euler17.pi This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => foreach(N in 1..10) println(english(N)) end, nl. go2 => foreach(I in 1..10) N = 1+(random2() mod 10**I), println([N,english(N)]) end, nl. english(N) = English => Divs = [1000000000, 1000000, 1000, 100], Divnames = ["billion", "million", "thousand", "hundred"], Prefixes = ["0", "twen", "thir", "for", "fif", "six", "seven", "eigh", "nine"], _Ordinals = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth","fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth"], Cardinals = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"], Sstr = "", Printed = 0, if N < 0 then Sstr := "minus" ++ Sstr, N := -N end, foreach(I in 1..Divs.length) D = N div Divs[I], N := N mod Divs[I], if D != 0 then Sstr := Sstr ++ english(D) ++ Divnames[I], Printed := 1 end end, if N > 0, Printed = 1 then Sstr := Sstr ++ " and " end, if N == 0 then 1 == 1 % dummy elseif N > 19 then D = N div 10, N := N mod 10, Sstr := Sstr ++ Prefixes[D] ++ "ty" ++ english(N) else Sstr := Sstr ++ Cardinals[N] end, English = Sstr.