# James Michael Hare

...hare-brained ideas from the realm of software development...
posts - 166 , comments - 1431 , trackbacks - 0

### News

Welcome to my blog! I'm a Sr. Software Development Engineer in the Seattle area, who has been performing C++/C#/Java development for over 20 years, but have definitely learned that there is always more to learn!

All thoughts and opinions expressed in my blog and my comments are my own and do not represent the thoughts of my employer.

## 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.

Print | posted on Monday, May 11, 2015 4:49 PM |