posted 10 years ago

OK, this is not really an advanced Java question, its more of an advanced algorithms question.

Everyone knows how to convert to and from Roman numerals from Arabic numbers. Its typically a CS101 topic.

When I was studying for my PhD, one of my commitee members mentioned that the clever way to do the conversion was to notice that Roman numerals have dual radix. While most programmers can think in binary or hex in addition to decimal, each of these has only a single radix (2, 16 or 10).

If you look a the standard converstion table, it looks like:

Notice that the I, X, C, and M are essentially decimal, but the V, L, and C are used for digits near 5. So its nearly a base 10 and base 5 system.

What I never found out was the clever way to use this to have code more elegant than the usual switch statements.

Anybody seen an implementation?

Everyone knows how to convert to and from Roman numerals from Arabic numbers. Its typically a CS101 topic.

When I was studying for my PhD, one of my commitee members mentioned that the clever way to do the conversion was to notice that Roman numerals have dual radix. While most programmers can think in binary or hex in addition to decimal, each of these has only a single radix (2, 16 or 10).

If you look a the standard converstion table, it looks like:

Notice that the I, X, C, and M are essentially decimal, but the V, L, and C are used for digits near 5. So its nearly a base 10 and base 5 system.

What I never found out was the clever way to use this to have code more elegant than the usual switch statements.

Anybody seen an implementation?

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 10 years ago

I haven't seen it, but I just made this. It seems to qualify, but's not necessarily the most elegant solution:

The parseRoman() method doesn't do any input validation, but the toRoman() method does, since it was simple. The toRoman() method still has several magic numbers in it that could be derived from other things, but it didn't seem necessary to remove them. Unless you want to generalize the solution to handle additional symbols for larger numbers, or other dual-radix systems.

[ September 03, 2007: Message edited by: Jim Yingst ]

The parseRoman() method doesn't do any input validation, but the toRoman() method does, since it was simple. The toRoman() method still has several magic numbers in it that could be derived from other things, but it didn't seem necessary to remove them. Unless you want to generalize the solution to handle additional symbols for larger numbers, or other dual-radix systems.

[ September 03, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, *Twister*