Home » Resources » Speakers Corner
Speakers CornerOpen to all. Sound off - change the world. Click here for info.
| Thursday, January 23, 2003 |
On the Conversion between Roman Numerals and Arabic Numbers
by Kevin Thompson
Some years ago, I was doing some work in the area of electronic document
imaging, and came across an interesting little problem: Some page numbers in
books are given in Roman numerals, but I needed to convert these to Arabic
numbers.
The rules for Roman numerals are tedious: 1, 2, and 3 are 'I', 'II', and
'III', but 4 is one less than 5, so the rule is that if 5 is represented by
'V', then 4 is 'IV', the 'I' preceding the 'V' indicating that 1 is
subtracted from 5. This pattern repeats as you go to larger numbers: 'X' for
10, 'IX' for 9, 'L' for 50, 'LX' for 40, and so on.
The thought of writing all the tests and conditional branches required to
embody the clumsy logic of Roman numerals was unappealing. What I really
wanted, of course, was a positional notation such as Arabic numerals, but
Roman numerals didn't obey a positional notation.
Or did they? After thinking about the idea of positional notations, I
realized that Roman numerals could be interpreted as a positional system,
albeit with some odd quirks. The two key concepts required to make the leap
to positional thinking were
1) 'Roman digits' do exist, but are multi-character sequences.
2) Roman digits are different for different powers of ten.
Suddenly everything clicked, and I realized that converting between Roman
numerals and Arabic numbers could be done purely through lookup tables, with
no clumsy logic involved. The algorithms to perform these lookup conversions
are given below. I have not seen them published anywhere, although Ian
Oliver, author of 'Programming Classics: Implementing the World's Best
Algorithms' expressed some interest in incorporating them in the next
edition of his book (I had bought the first edition). I haven't checked to
see if he has.
Next »
|