Last week we converted integers into Roman numerals, and I promised this week we would write a function to do the opposite. Part of the difficulty in working with Roman numerals is the subtractive notation. The number four can be written as IV or IIII; writing it as IV is the subtractive notation. When I precedes V, it means one less than five; if you reverse the two (VI), it means one more than five.
To make our life easier, we want to expand the subtractive notation so that each numeral is equal to its value. While we're at it, we'll also write a function to compress Roman numerals back in subtractive notation.
ASP
function roman_expand(ByVal roman)
roman = replace(roman, "CM", "DCCCC")
roman = replace(roman, "CD", "CCCC")
roman = replace(roman, "XC", "LXXXX")
roman = replace(roman, "XL", "XXXX")
roman = replace(roman, "IX", "VIIII")
roman = replace(roman, "IV", "IIII")
roman_expand = roman
end function
function roman_compress(ByVal roman)
roman = replace(roman, "DCCCC", "CM")
roman = replace(roman, "CCCC", "CD")
roman = replace(roman, "LXXXX", "XC")
roman = replace(roman, "XXXX", "XL")
roman = replace(roman, "VIIII", "IX")
roman = replace(roman, "IIII", "IV")
roman_compress = roman
end function
PHP
function roman_expand($roman)
{
$roman = str_replace("CM", "DCCCC", $roman);
$roman = str_replace("CD", "CCCC", $roman);
$roman = str_replace("XC", "LXXXX", $roman);
$roman = str_replace("XL", "XXXX", $roman);
$roman = str_replace("IX", "VIIII", $roman);
$roman = str_replace("IV", "IIII", $roman);
return $roman;
}
function roman_compress($roman)
{
$roman = str_replace("DCCCC", "CM", $roman);
$roman = str_replace("CCCC", "CD", $roman);
$roman = str_replace("LXXXX", "XC", $roman);
$roman = str_replace("XXXX", "XL", $roman);
$roman = str_replace("VIIII", "IX", $roman);
$roman = str_replace("IIII", "IV", $roman);
return $roman;
}
These expand and compress functions would be essential if you wanted to write some functions to actually perform arithmetic on Roman numerals, and although on paper Roman addition is fairly straightforward, an implementation of it in code performs more poorly than simply converting the Roman numeral back into an integer and using the built-in math operators.
The following ASP function requires the InstrCount function from my previous post.
ASP
function arabic(ByVal roman)
Dim result
result = 0
' Remove subtractive notation.
roman = roman_expand(roman)
' Calculate for each numeral.
result = result + InstrCount(roman, "M") * 1000
result = result + InstrCount(roman, "D") * 500
result = result + InstrCount(roman, "C") * 100
result = result + InstrCount(roman, "L") * 50
result = result + InstrCount(roman, "X") * 10
result = result + InstrCount(roman, "V") * 5
result = result + InstrCount(roman, "I")
end function
PHP
function arabic($roman)
{
$result = 0;
// Remove subtractive notation.
$roman = roman_expand($roman);
// Calculate for each numeral.
$result += substr_count($roman, 'M') * 1000;
$result += substr_count($roman, 'D') * 500;
$result += substr_count($roman, 'C') * 100;
$result += substr_count($roman, 'L') * 50;
$result += substr_count($roman, 'X') * 10;
$result += substr_count($roman, 'V') * 5;
$result += substr_count($roman, 'I');
return $result;
}
Although I don't have code for you to actually add two Roman numerals together, I will tell you how to do it. For this example, we will add the numerals XIV and VII. First we must remove the subtractive notation, so XIV becomes XIIII and VII stays as it is. On paper we would then concatenate the two together and sort them.
XIIII + VII = XIIIIVII = XVIIIIII
We already have a concatenation operator, but we would need to write a custom sorting function for Roman numerals. If we're going to go that much trouble, it might be better to just build a new string by counting the number of each numeral in the two strings we are given. This would allow us to handle the situation above where we now have six I's. Our next step is to combine similar numerals that are equal to the next larger numeral.
XVIIIIII = XVVI = XXI
In code, it would be best to start with the smallest numeral, I, and work our way towards the largest numeral.
Subtraction of Roman numerals involves expanding and cancelling until there is only one number left.
XIV - VI = XIIII - VI = XIII - V = VVIII - V = VIII
I leave it up to you to research how to perform multiplication and division of Roman numerals, and whether or not you want to write functions for Roman arithmetic. Such functions would not be useful in production code, but writing them would be a good exercise, and it would be interesting to compare the runtimes with conversion back to Arabic followed by a conversion to Roman again.
Next weekend is Easter, so we'll be writing code to calculate when Easter and its related holidays (Good Friday, Pentecost, etc.) occur for any given year.