Overview
Adding slightly to the complexity of working with Roman numerals...
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 sold 27 of them, 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.
Objective
Write the shortest algorithm/function/program that will subtract two Roman numerals together and output the result without converting the string representation of either input into a number.
Rules/Constraints
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.
- Since we are subtracting, negative numbers are permissible. 0s, as they have no representation in the Roman system, should be returned as
NULL
or an empty string. - 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 == 9
butIX != 9
and 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.
I do have a solution to this problem that has already been beaten, but I'll hold off on posting it for a few days to see if any other answers come up.
Value Guide
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
Examples
XII - VIII = IIII (12 - 8 = 4)
MCCXXII - MCCXXII = '' (2,222 - 2,222 = 0)
XXIIII - XXXXII = -XVIII (24 - 42 = -18)
If any further clarification is needed, please ask.
IX
is invalid? Those aren't Roman numerals then, are they? \$\endgroup\$IV
is generally used in place ofIIII
. \$\endgroup\$