More Roman Sums

Here is a list of 100 integers expressed in Roman Numerals. Find their sum.

Note : This problem does deal with subtractive notation. You might want to solve this problem which does not.


The answer is 97078.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Python:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    def rom_to_int(string):

        table=[['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]]
        returnint=0
        for pair in table:


            continueyes=True

            while continueyes:
                if len(string)>=len(pair[0]):

                    if string[0:len(pair[0])]==pair[0]:
                        returnint+=pair[1]
                        string=string[len(pair[0]):]

                    else: continueyes=False
                else: continueyes=False

        return returnint

    alist = ['CDLIX','CMLII','DLVII','MCCCXLVII','CCCXV','MDCV','DCCXXVI','XXXVI','CMLXXXVI','DXCIII','CCCLIII','MDCXIV','MCCCLX','MDCCCLXXXIV','CI','CXCII','MCCLVI','LXXXII','CMLXVI','DCCXVII','MCVII','MCCLXXI','DLXXVIII','LXV','CDLXVIII','CXIII','MCXII','MDCLIII','MDXCV','XCVIII','CMXVII','CMVIII','MCLXV','CC','CDXXIV','MCDXCIV','DCCLXXXI','DXXX','DCCXXXVII','MCMXXXII','CLXIX','MCDXXII','MDXXI','MDCCXCVI','MCDII','MCCLV','MLXXI','MCMLXIX','XVI','MDCCXXX','MDCCVII','DXXVII','CCXXX','MCCCXCVI','MCDXLVI','CMXXVII','MDXCIX','CMXLV','CCXXI','MCMXII','MDCXXXV','CMII','MCCCXCI','MDXV','MCCCLXXXIX','CDLXVII','MCDLXXIV','DCCLIII','CDXCI','D','CIV','MCCCXI','CCCLXXXVII','DCCXXXIII','CDLXXXII','DCLXXI','MCCCX','MLXXI','DXL','DCCLXXXV','MCDXXXV','MC','MCLXIX','DXII','DLXXVI','DXXVI','DCCCXXXIX','MXVIII','MDCIII','CDXII','MCDXCVII','MDCCXXV','MCLXII','MCCIII','MDCXXXII','CDLXXXVI','MDCCLXX','MDCLI','MCMLXXIX','CCCLXII']
    blist = []
    for a in alist:
        blist.append(rom_to_int(a))

    print (sum(blist)) 

Nice. This was the kind of solution I was expecting. Hope it was not too tedious.

I fixed your solution to reflect syntax coloring

Agnishom Chattopadhyay - 4 years, 11 months ago
Peleg Tsadok
Jul 28, 2016

This is C#:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
static int ConvertLetter(char letter)
{
    string roms = "IVXLCDM";
    int[] values = { 1, 5, 10, 50, 100, 500, 1000 };
    for(int i = 0; i < roms.Length; i++)
    {
        if (letter == roms[i])
        return values[i];
    }
    return 0;
}

static int ConvertToArabic(string rom)
{
    int num = 0;
    num = num + ConvertLetter(rom[rom.Length - 1]);
    for (int i = rom.Length - 2; i >= 0; i--)
    {
        if (ConvertLetter(rom[i]) < ConvertLetter(rom[i + 1]))
            num = num - ConvertLetter(rom[i]);
        else
            num = num + ConvertLetter(rom[i]);
    }
    return num;
}

static void Main()
{
    string[] roms = { "CDLIX", "CMLII", "DLVII", "MCCCXLVII", "CCCXV", "MDCV", "DCCXXVI", "XXXVI", "CMLXXXVI", "DXCIII", "CCCLIII", "MDCXIV", "MCCCLX", "MDCCCLXXXIV", "CI", "CXCII", "MCCLVI", "LXXXII", "CMLXVI", "DCCXVII", "MCVII", "MCCLXXI", "DLXXVIII", "LXV", "CDLXVIII", "CXIII", "MCXII", "MDCLIII", "MDXCV", "XCVIII", "CMXVII", "CMVIII", "MCLXV", "CC", "CDXXIV", "MCDXCIV", "DCCLXXXI", "DXXX", "DCCXXXVII", "MCMXXXII", "CLXIX", "MCDXXII", "MDXXI", "MDCCXCVI", "MCDII", "MCCLV", "MLXXI", "MCMLXIX", "XVI", "MDCCXXX", "MDCCVII", "DXXVII", "CCXXX", "MCCCXCVI", "MCDXLVI", "CMXXVII", "MDXCIX", "CMXLV", "CCXXI", "MCMXII", "MDCXXXV", "CMII", "MCCCXCI", "MDXV", "MCCCLXXXIX", "CDLXVII", "MCDLXXIV", "DCCLIII", "CDXCI", "D", "CIV", "MCCCXI", "CCCLXXXVII", "DCCXXXIII", "CDLXXXII", "DCLXXI", "MCCCX", "MLXXI", "DXL", "DCCLXXXV", "MCDXXXV", "MC", "MCLXIX", "DXII", "DLXXVI", "DXXVI", "DCCCXXXIX", "MXVIII", "MDCIII", "CDXII", "MCDXCVII", "MDCCXXV", "MCLXII", "MCCIII", "MDCXXXII", "CDLXXXVI", "MDCCLXX", "MDCLI", "MCMLXXIX", "CCCLXII" };
    int sum = 0;
    for (int i = 0; i < roms.Length; i++)
        sum = sum + ConvertToArabic(roms[i]);
    Console.WriteLine(sum);
}

Rushikesh Jogdand
Jul 12, 2016

Convert numbers in subtractive notation to those not in subtractive notation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
with open('raw.txt','r') as file:
    exec('RomanSub='+file.read())
RomanToInt={'D': 500, 'M': 1000, 'I': 1, 'V': 5, 'C': 100, 'X': 10, 'L': 50}
MakeNoSub={'CD': 'CCCC', 'IV': 'IIII', 'IX': 'VIIII', 'XC': 'LXXXX', 'XL': 'XXXX', 'CM': 'DCCCC'}
RomanNoSub=[]
for i in RomanSub:
    tmp=i+''
    for j in ['CM','CD','XC','XL','IX','IV']:
        tmp=tmp.replace(j,MakeNoSub[j])
    RomanNoSub.append(tmp)
su=0
for i in RomanNoSub:
    for j in list(i):
        su+=RomanToInt[j]
print(su)

Michael Mendrin
Jul 8, 2016

Ugh, took a few tries.

Mind sharing your code with us?

Agnishom Chattopadhyay - 4 years, 11 months ago

Log in to reply

Code? Here's what I did. First, I took your raw text and dumped it into Microsoft Word. Then I replaced all the {M,D,C,L,X,V,I} with {1000,500,100,50,10,5,1}, plus putting in commas and brackets in all the right places, using the "Replace" feature. Then I put this into Mathematica which accepts it as a regular array. Then whenever there is a pair of numbers such that the first is less than the other, the other is reduced by the first, while the first is made 0. Then flatten all, and then total all.

What code? It was almost done by hand. Being that this is supposed to be "computer science", I suppose I cheated.

Michael Mendrin - 4 years, 11 months ago

Log in to reply

That's cool. How did you manage the occurences of IX, IV, etc?

Agnishom Chattopadhyay - 4 years, 11 months ago

Log in to reply

@Agnishom Chattopadhyay The arrays, once prepared for Mathematica, shows numbers in descending order, except for those subtractive notation occurrences. I used a simple program to go look for those pairs of numbers not in descending order and make simple arithmetic adjustments. For example, if CM, then that means (-100+1000). I could have just made the 100 into a negative -100 with the same results, but I was too much of a hurry to be that clever, and instead changed the pair into (0, 900). Probably out of superstitious need to stay with the familiar?

Michael Mendrin - 4 years, 11 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...