Solution to Little Puzzlers–Positive Integer to Roman Numerals

This is the way I went about the "Positive Integer to Roman Numerals” problem. However, keep in mind there are multiple ways to solve this, so don't worry if your solution has variations and it’s entirely possible there are more efficient ways.

Feel free to suggest your solution in the comments here or in the original post, but please be respectful of others efforts.

My Approach

My approach was fairly straight-forward.  I simply built a “table” of denominations to the symbol that represents that denomination and arranged it in descending order by denomination.  In this way, I can just loop through the denominations, and as long as the remainder is > than the denomination, add the symbol and decrement the remainder.  Rinse and repeat!

The nice thing about this approach is it is fairly straightforward and can use some fairly optimal constructs (such as array iteration and StringBuilder for constructing a string over multiple statements).

In addition, the fact that the 4s and 9s (4, 9, 40, 90, 400, 900…) helps simplify the program greatly.

1: public class RomanNumerals

2: {

3: private class Denomination

4: {

5: public int Value { get; set; }

6: public string Symbol { get; set; }

7: }

8: 

9: private static readonly Denomination[] table =

10: {

11: new Denomination { Value = 1000, Symbol = "M" },

12: new Denomination { Value = 900, Symbol = "CM" },

13: new Denomination { Value = 500, Symbol = "D" },

14: new Denomination { Value = 400, Symbol = "CD" },

15: new Denomination { Value = 100, Symbol = "C" },

16: new Denomination { Value = 90, Symbol = "XC" },

17: new Denomination { Value = 50, Symbol = "L" },

18: new Denomination { Value = 40, Symbol = "XL" },

19: new Denomination { Value = 10, Symbol = "X" },

20: new Denomination { Value = 9, Symbol = "IX" },

21: new Denomination { Value = 5, Symbol = "V" },

22: new Denomination { Value = 4, Symbol = "IV" },

23: new Denomination { Value = 1, Symbol = "I" }

24: };

25: 

26: public static string Translate(int number)

27: {

28: var result = new StringBuilder();

29: 

30: int remainder = number;

31: 

32: foreach (var current in table)

33: {

34: while (remainder >= current.Value)

35: {

36: result.Append(current.Symbol);

37: remainder -= current.Value;

38: }

39: }

40: 

41: return result.ToString();

42: }

43: }

Check out the comments in the original post for more interesting solutions as well!

Summary

Hope you had fun with this one!  Of course, I’m sure many out there can tweak the answer for performance in various ways – but you get the point.

Have a solution that worked for you but was totally different?  I’d love to hear about it!

Stay tuned next week for the next Little Puzzler.

This article is part of the GWB Archives. Original Author: James Michael Hare

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.