The ancient Romans devised a number system using Latin letters, which served them well, and which is still used by modern civilization, though to a much smaller degree. In the time of its use, Romans would have had to learn to use and manipulate these numbers in order to be of much use for many applications. For example, if a man owned 35 oxen, and he procured 27 more, how would he know the new total other than counting them all? (Ok, that and using an abacus...) If the Romans could do it, surely we can figure it out as well.
Write the shortest algorithm/function/program that will add two Roman numerals together and output the result without converting the string representation of either input into a number.
Because of historical/pre-medieval inconsistencies in formatting, I'm going to outline some non-standard (per modern usage) rules for orthography. See the value guide below as an example.
- The letters I, X, C, and M can be repeated up to four times in succession, but no more. D, L, and V can never be repeated.
- The letter immediately to the right of another letter in the Roman representation will be of the same or lesser value than that to its left.
- In other words,
VIIII == 9but
IX != 9and is invalid/not allowed.
- In other words,
- All input values will be 2,000 (MM) or less; no representation for numbers greater than M is necessary.
- All input values will be a valid Roman numeral, pursuant to the rules above.
- You may not convert any numbers to decimal, binary, or any other number system as part of your solution (you are welcome to use such a method to VERIFY your results).
- This is code golf, so shortest code wins.
Symbol Value I 1 II 2 III 3 IIII 4 V 5 VIIII 9 X 10 XIIII 14 XXXXIIII 44 L 50 LXXXXVIIII 99 C 100 D 500 M 1,000
XII + VIII = XX (12 + 8 = 20) MCCXXII + MCCXXII = MMCCCCXXXXIIII (1,222 + 1,222 = 2,444) XXIIII + XXXXII = LXVI (24 + 42 = 66)
If any further clarification is needed, please ask.