• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

two radix solution to convert Roman numerals

Posts: 4686
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Garden Master Course kickstarter
    Bookmark Topic Watch Topic
  • New Topic