Project Euler Problem 89 Solution


The rules for writing Roman numerals allow for many ways of writing each number (see About Roman Numerals…). However, there is always a “best” way of writing a particular number.

For example, the following represent all of the legitimate ways of writing the number sixteen:


The last example being considered the most efficient, as it uses the least number of numerals.

The 11K text file, roman.txt (right click and ‘Save Link/Target As…’), contains one thousand numbers written in valid, but not necessarily minimal, Roman numerals; that is, they are arranged in descending units and obey the subtractive pair rule (see About Roman Numerals… for the definitive rules for this problem).

Find the number of characters saved by writing each of these in their minimal form.

Note: You can assume that all the Roman numerals in the file contain no more than four consecutive identical units.


#!/usr/bin/env python
import os

def subtractive(roman):
    result = roman
    replacements = [
        ("VIIII", "IX"), 
        ("IIII", "IV"), 
        ("LXXXX", "XC"), 
        ("XXXX", "XL"),
        ("DCCCC", "CM"), 
        ("CCCC", "CD"),
    for old, new in replacements:
        result = result.replace(old, new)
    return result

current = 0
improved = 0
for line in open(os.path.join(os.path.dirname(__file__), 'roman.txt')):
    roman = line.strip()
    current += len(roman)
    improved += len(subtractive(roman))
print(current - improved)
$ time python3
real   0m0.018s
user   0m0.016s
sys    0m0.000s