## Roman Numerals

### March 6, 2009

Roman numerals are a number-notation system developed in classical Rome, chiefly used today to indicate the year in which a motion picture was made, or the sequence number of a Super Bowl.

Roman numerals use letters of the alphabet to indicate numerical value, according to the following code:

I 1 V 5 X 10 L 50 C 100 D 500 M 1000

For example, the number 1732 is represented by Roman numerals as MDCCXXXII, and the number 1956 is represented by Roman numerals as MDCCCCLVI. Letter symbols are normally written from the largest symbol to the smallest, left to right, so the numeric values are additive. However, in order to conserve space, it is permissible to replace four of the same symbol written all in a row in a subtractive manner to the left of a higher-value symbol, so that 1956 may also be represented as MCMLVI, where the CM symbol, with C before M, indicates that C is subtracted from M, and thus indicates the numeric value 900. Wikipedia and MathWorld explain the common usage of Roman numerals.

Your task is to write a function that takes two roman numerals (character strings) as input and returns their sum as a roman numeral as output. Be sure that input can be given in either the additive or subtractive forms of Roman numerals; give output using the subtractive form. What is `add-roman("CCCLXIX", "CDXLVIII")`

?

Pages: 1 2

I started to program thinking that constructions like “IIX” for 8 are allowed. Wikipedia says that this kind of subtractive notation exists, but it seems very rare. Whatever, here is my roman->decimal.

Haskell (someone more experienced than me can probably turn these into one-liners):

import Data.Map (fromList, (!))

import Data.Char

import Data.List

data Roman = I | V | X | L | C | D | M deriving (Enum, Eq, Ord, Read, Show)

main = print $ addRoman “CCCLXIX” “CDXLVIII”

values :: [(Roman, Int)]

values = [(M, 1000), (D, 500), (C, 100), (L, 50), (X, 10), (V, 5), (I, 1)]

fromRoman :: String -> Int

fromRoman = fromRoman’ . map (read . return) where

fromRoman’ (x:y:xs) = (if x String

toRoman = map toLower . concatMap show . subtractiveStyle . toRoman’ values where

toRoman’ [] _ = []

toRoman’ ((r, v):xs) n = replicate (div n v) r ++ toRoman’ xs (mod n v)

subtractiveStyle :: [Roman] -> [Roman]

subtractiveStyle (x:y:ys) | y == pred x && isPrefixOf [y,y,y] ys

= y : succ x : subtractiveStyle (drop 3 ys)

subtractiveStyle xs = xs

addRoman :: String -> String -> String

addRoman a b = toRoman $ fromRoman a + fromRoman b

Evidently there’s only a limited selection of languages that will trigger the right formatting. My apologies.

FalconNL: Haskell is not one of the supported languages for the WordPress sourcecode tag. See my HOWTO page for more about posting source code in comments.

At first I thought my solution was long and ugly and than I read the praxis’ Scheme solution :)

I read Roger’s Scheme but it is incomplete : no encode-roman

I read FalconNL’s Haskell and subtractiveStyle doesn’t work on numbers like 1904

So I guess I’ll roll my ugly clojure solution :

Some use cases :

Hello everybody,

I briefly walked through all posted examples and figured out that noone completely solved this task.

kawas was very close to solution however even in his second use case user=> (add-roman “MMCCCII” “MMDCII”) result seemed to be wrong because there cannot be 4 M in a row. Please see wikipedia as a proof-link http://en.wikipedia.org/wiki/Roman_numerals

So here is my solution in Python:

Results:

Roman sum: CCCLXIX + CDXLVIII = DCCCXVII

Arab sum(verification): 369 + 448 = 817

Roman sum: CDXXVIII + DLXXVIII = MVI

Arab sum(verification): 428 + 578 = 1006

Roman sum: MDCCL + MDCLXX = MMMCDXX

Arab sum(verification): 1750 + 1670 = 3420

Cheers,

Pavel

A nice additional constraint to the problem — at least for Roman numerals in additive form — is to forbid conversion back to decimal for the purposes of carrying out the addition. This seems more authentic, given that conversion to decimal was not an option available to the classical Romans!

I discuss this approach to a solution along with some other (arguably) interesting connections on my new blog. Straight to the Python code.

How to chose between VIV and IX? I can’t seem to think of a way to follow this convention.

In coffeescript https://gist.github.com/4576731

See my code at: https://gist.github.com/4606068

https://github.com/ftt/programming-praxis/blob/master/20090306-roman-numerals/roman-numerals.py