# Roman Numerals

From reddit.com/r/programingchallenges:

I googled this and I haven’t found a similar challenge, so I’d like to pose this question to you all!

Let’s say I give you a range from 1 to 2000. Within this range, find the number that yields the most characters. I asked a friend of mine and he worked out that 1888 has a lot of characters (MDCCCLXXXVIII).

### Solution

```
import time
SYMBOLS = [
('M', 1000),
('CM', 900),
('D', 500),
('CD', 400),
('C', 100),
('XC', 90),
('L', 50),
('XL', 40),
('X', 10),
('IX', 9),
('V', 5),
('IV', 4),
('I', 1)]
def roman_numeral(number):
roman_number = [];
for (symbol, value) in SYMBOLS:
while value <= number:
roman_number.append(symbol)
number -= value
return ''.join(roman_number);
start = time.time();
pairs = [(i, roman_numeral(i)) for i in range(1, 2000)]
pairs.sort(lambda a,b: cmp(len(a[1]), len(b[1])))
print 'Longest roman numeral for numbers 1-2000 = %d -> %s' % \
(pairs[-1][0], pairs[-1][1])
print 'Took: %.2fsec' % (time.time() - start,)
```

### Output

```
Longest roman numeral for numbers 1-2000 = 1888 -> MDCCCLXXXVIII
Took: 0.14sec
```

### Notes

Turns out historically there wasn’t a strict set of rules for Roman numerals, for example IV and IIII are both valid representations of the number 4. Only recent rules have added limits on the number of repeated characters and what values can be subtracted from other values. [Reference][2].

[2]: http://en.wikipedia.org/wiki/Roman_numerals#Reading Roman numerals