Roman numerals/Encode: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 295: Line 295:


<ocaml>
<ocaml>
let digit x y z k =
let digit x y z = function
1 -> [x]
List.nth
| 2 -> [x;x]
[[x];[x;x];[x;x;x];[x;y];[y];[y;x];[y;x;x];[y;x;x;x];[x;z]]
(k - 1)
| 3 -> [x;x;x]
| 4 -> [x;y]
| 5 -> [y]
| 6 -> [y;x]
| 7 -> [y;x;x]
| 8 -> [y;x;x;x]
| 9 -> [x;z]


let rec to_roman x =
let rec to_roman x =

Revision as of 00:52, 11 April 2008

Task
Roman numerals/Encode
You are encouraged to solve this task according to the task description, using any language you may know.

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.

Ada

<Ada> with Ada.Text_Io; use Ada.Text_Io; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure Roman_Numeral_Test is

  function To_Roman(Number : Positive) return String is
     Result : Unbounded_String;
     Current_Value : Natural := Number;
  begin
     if Current_Value >= 1000 then
        Result := Result & ((Current_Value / 1000) * 'M');
        Current_Value := Current_Value mod 1000;
     end if;
     if Current_Value >= 900 then
        Result := Result & "CM";
        Current_Value := Current_Value mod 900;
     end if;
     if Current_Value >= 500 then
        Result := Result & "D";
        Current_Value := Current_Value mod 500;
     end if;
     if Current_Value >= 100 then
        Result := Result & ((Current_Value / 100) * 'C');
        Current_Value := Current_Value mod 100;
     end if;
     if Current_Value >= 90 then
        Result := Result & "XC";
        Current_Value := Current_Value mod 90;
     end if;
     if Current_Value >= 50 then
        Result := Result & "L";
        Current_Value := Current_Value mod 50;
     end if;
     if Current_Value >= 40 then
        Result := Result & "XL";
        Current_Value := Current_Value mod 40;
     end if;
     if Current_Value >= 10 then
        Result := Result & ((Current_Value / 10) * 'X');
        Current_Value := Current_Value mod 10;
     end if;
     if Current_Value = 9 then
        Result := Result & "IX";
        Current_Value := 0;
     end if;
     if Current_Value >= 5 then
        Result := Result & "V";
        Current_Value := Current_Value mod 5;
     end if;
     if Current_Value = 4 then
        Result := Result & "IV";
        Current_Value := 0;
     end if;
     Result := Result & (Current_Value * 'I');
     return To_String(Result);
  end To_Roman;

begin

  Put_Line(To_Roman(1999));
  Put_Line(To_Roman(25));
  Put_Line(To_Roman(944));

end Roman_Numeral_Test; </Ada> Output:

MCMXCIX
XXV
CMXLIV

D

This implementation in generally follows the rules implied by Modern Roman numerals, with some irregularity depend on whether numerals larger than M(1000) is used, eg. 4000 is converted to MV' if V' is used, MMMM if not. <d>module roman ; import std.stdio ;

const string[] Roman = ["V","X","L","C","D","M","I"] ; const int RLen = Roman.length - 1 ; const int[][] RDigit =

 [[0],[0,0],[0,0,0],[0,1],[1],[1,0],[1,0,0],[1,0,0,0],[0,2],[0,0,0,0]] ;

const string[] Power = ["", "'","\"","`","~","^","#"] ; // arbitary _power_ symbols, or

           // Power = ["1","2","3","4","5","6","7"] ;  // for easier further processing

const int[][] Shift = [[0,0,0],[-1,0,0]] ;

string romanPart(int n, int part, bool extented) {

 if (n == 0) return "" ;
 int[3] b ;  
 b[1] = (2 * part) % RLen ;
 b[0] = part == 0 ? RLen : (RLen + b[1] - 1) % RLen ;
 b[2] = b[1] + 1 ;
 int power = part / 3 ;
 int[] shift = Shift[ b[1] == 0 && part != 0 ? 1 : 0] ;
 int[] Digit = !extented && n == 4 && part == 3 ? RDigit[$-1]  : RDigit[n-1]  ;
 string res ;
 foreach(inx ; Digit)
   res ~= Roman[b[inx]] ~ Power[power + shift[inx]] ;
 return res ;

} string toRoman(long n, bool extented = true) {

 if(n < 0) throw new Exception("No negative Roman Numeral") ;
 if(n == 0) return "" ;
 if(!extented && n >= 5000) throw new Exception("Only smaller than 5000 allowed") ;
 string romans ;
 int part = 0 ;
 while (n > 0) {
   long m = n / 10 ;
   romans = romanPart(n - m*10, part, extented) ~ romans ;
   n = m ;
   part++ ;
 }
 return romans ;

} void main() {

 auto test = [1L,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,25,30,40,50,60,69,70,
   80,90,99,100,200,300,400,500,600,666,700,800,900,1000,1009,1444,1666,1945,1997, 1999,
   2000,2008,2500,3000,4000,4999,5000,6666,10000,50000,100000,500000,1000000,long.max] ;
 foreach(x ; test)   
   writefln("%20s - %s", x, toRoman(x)) ;

}</d>

Forth

: vector create ( n -- ) 0 do , loop
         does>  ( n -- ) swap cells + @ execute ;

\ these are all ( numerals -- )
:noname dup c@ emit 2 + c@ emit                  ; \ 9: IX
:noname dup 1+ c@ emit c@ dup dup emit emit emit ; \ 8: VIII
:noname dup 1+ c@ emit c@ dup          emit emit ; \ 7: VII
:noname dup 1+ c@ emit c@                   emit ; \ 6: VI
:noname     1+ c@ emit                           ; \ 5: V
:noname dup c@ emit 1+ c@ emit                   ; \ 4: IV
:noname                c@ dup dup emit emit emit ; \ 3: III
:noname                c@ dup          emit emit ; \ 2: II
:noname                c@                   emit ; \ 1: I
' drop                                             \ 0: no output

10 vector .digit

: roman-rec ( numerals n -- )
  10 /mod dup if >r over 2 + r> recurse else drop then .digit ;

: .roman ( n -- )
  dup 0 4000 within 0= if ." EX LIMITO!" exit then
  s" IVXLCDM" drop swap roman-rec ;

Haskell

With an explicit decimal digit representation list:

digit x y z k = 
  [[x],[x,x],[x,x,x],[x,y],[y],[y,x],[y,x,x],[y,x,x,x],[x,z]] !! 
  (fromInteger k - 1)

toRoman :: Integer -> String
toRoman 0 = ""
toRoman x | x < 0     = error "Negative roman numeral"
toRoman x | x >= 1000 = 'M' : toRoman (x - 1000)
toRoman x | x >= 100  = digit 'C' 'D' 'M' q ++ toRoman r where 
  (q,r) = x `divMod` 100
toRoman x | x >= 10   = digit 'X' 'L' 'C' q ++ toRoman r where 
  (q,r) = x `divMod` 10
toRoman x             = digit 'I' 'V' 'X' x

Output:

*Main> map toRoman [1999,25,944]
["MCMXCIX","XXV","CMXLIV"]

J

rfd obtains Roman numerals from decimals, and dfr decimals from Roman numerals.

dfr=: 3 : 0
 i=. 'IVXLCDM' i. y
 d=. i{1 5 10 50 100 500 1000
 +/d*_1^i<}.i,_1
)

r100 =. <;._1 '  C CC CCC CD D DC DCC DCCC CM'
r10  =. <;._1 '  X XX XXX XL L LX LXX LXXX XC'
r1   =. <;._1 '  I II III IV V VI VII VIII IX'
R1000=: , r100 ,&.>/ r10 ,&.>/ r1

rfd=: 3 : 0
 ('M'$~<.y%1000),R1000{::~1000|y
)

Copied, with permission, from the J Wiki. Examples of use will be found there.

Java

Translation of: Ada

The helper function copies is added since Java does not support String multiplication. The conversion function returns null for a negative number, since Java does not have unsigned primitives. <java>public class RN{ public static void main(String args[]){ System.out.println(roman(1999)); System.out.println(roman(25)); System.out.println(roman(954)); } public static String roman(long n){ if(n < 1) return null; String result = ""; if(n >= 1000){ result+= (copies("M",(n / 1000))); n%= 1000; } if(n >= 900){ result+= "CM"; n%= 900; } if(n >= 500){ result+= "D"; n%= 500; } if(n >= 400){ result+= "CD"; n%= 400; } if(n >= 100){ result+= (copies("C",(n / 100))); n%= 100; } if(n >= 90){ result+= "XC"; n%= 90; } if(n >= 50){ result+= "L"; n%= 50; } if(n >= 40){ result+= "XL"; n%= 40; } if(n >= 10){ result+= (copies("X",(n / 10))); n%= 10; } if(n == 9){ result+= "IX"; n= 0; } if(n >= 5){ result+= "V"; n%= 5; } if(n == 4){ result+= "IV"; n= 0; } result+= (copies("I",n)); return result; }

public static String copies(String a, int n){ String result = ""; for(int i= 0;i < n;i++,result+= a); return result; } }</java> Output:

MCMXCIX
XXV
CMXLIV

Works with: UCB Logo
make "patterns [[?] [? ?] [? ? ?] [? ?2] [?2] [?2 ?] [?2 ? ?] [?2 ? ? ?] [? ?3]]

to digit :d :numerals
  if :d = 0 [output "||]
  output apply (sentence "\( "word (item :d :patterns) "\)) :numerals
end
to digits :n :numerals
  output word ifelse :n < 10 ["||] [digits int :n/10 bf bf :numerals] ~
              digit modulo :n 10 :numerals
end
to roman :n
  if or :n < 0 :n >= 4000 [output [EX MODVS!]]
  output digits :n [I V X L C D M]
end
print roman 1999  ; MCMXCIX 
print roman 25    ; XXV
print roman 944   ; CMXLIV


OCaml

With an explicit decimal digit representation list:

<ocaml> let digit x y z = function

   1 -> [x]
 | 2 -> [x;x]
 | 3 -> [x;x;x]
 | 4 -> [x;y]
 | 5 -> [y]
 | 6 -> [y;x]
 | 7 -> [y;x;x]
 | 8 -> [y;x;x;x]
 | 9 -> [x;z]

let rec to_roman x =

 if x = 0 then []
 else if x < 0 then
   invalid_arg "Negative roman numeral"
 else if x >= 1000 then
   'M' :: to_roman (x - 1000)
 else if x >= 100 then
   digit 'C' 'D' 'M' (x / 100) @ to_roman (x mod 100)
 else if x >= 10 then
   digit 'X' 'L' 'C' (x / 10) @ to_roman (x mod 10)
 else
   digit 'I' 'V' 'X' x

</ocaml>

Output:

# to_roman 1999;;
- : char list = ['M'; 'C'; 'M'; 'X'; 'C'; 'I'; 'X']
# to_roman 25;;
- : char list = ['X'; 'X'; 'V']
# to_roman 944;;
- : char list = ['C'; 'M'; 'X'; 'L'; 'I'; 'V']

Perl

Works with: Romana::Perligata

Perligata outputs numbers in Arabic, but the verb come ("beautify") may be used to convert numbers to proper Roman numerals:

       per quisque in I tum C conscribementum sic
               hoc tum duos multiplicamentum comementum egresso scribe.
       cis

Ruby

Roman numeral generation was used as an example for demonstrating Test Driven Development in Ruby.