/* Roman numerals/Encode in Picat. From Rosetta code: http://rosettacode.org/wiki/Roman_numerals/Encode """ Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer. Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI. """ Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go => List = [455,999,1990,1999,2000,2001,2008,2009,2010,2011,2012,1666,3456,3888,4000], foreach(Val in List) printf("%4d: %w\n", Val, roman_encode(Val)) end, nl. go2 => All = [Len=I=roman_encode(I) : I in 1..4000,E=roman_encode(I), Len=E.len].sort_down, println(All[1..2]), nl. roman_encode(Val) = Res => if Val <= 0 then Res := -1 else Arabic = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1], Roman = ["M", "CM", "D", "CD", "C", "XC","L","XL","X","IX","V","IV","I"], Res = "", foreach(I in 1..Arabic.length) while(Val >= Arabic[I]) Res := Res ++ Roman[I], Val := Val - Arabic[I] end end end.