Case Study: Roman Numerals
We'll cover the following
You’ve most likely seen Roman numerals, even if you didn’t recognize them. You may have seen them in copyrights of old movies and television shows (“Copyright MCMXLVI
” instead of “Copyright 1946
”), or on the dedication walls of libraries or universities (“established MDCCCLXXXVIII
” instead of “established 1888
”). You may also have seen them in outlines and bibliographical references. It’s a system of representing numbers that really does date back to the ancient Roman empire (hence the name).
In Roman numerals, there are seven characters that are repeated and combined in various ways to represent numbers.
- I = 1
- V = 5
- X = 10
- L = 50
- C = 100
- D = 500
- M = 1000
The following are some general rules for constructing Roman numerals:
- Sometimes characters are additive.
I
is1
,II
is2
, andIII
is3
.VI
is6
(literally, “5
and1
”),VII
is7
, andVIII
is8
. - The tens characters (
I
,X
,C
, andM
) can be repeated up to three times. At4
, you need to subtract from the next highest fives character. You can’t represent4
asIIII
; instead, it is represented asIV
(“1 less than 5”).40
is written asXL
(“10
less than50
”),41
asXLI
,42
asXLII
,43
asXLIII
, and then44
asXLIV
(“10
less than50
, then1
less than5
”). - Sometimes characters are… the opposite of additive. By putting certain characters before others, you subtract from the final value. For example, at
9
, you need to subtract from the next highest tens character:8
isVIII
, but9
isIX
(“1
less than10
”), notVIIII
(since theI
character can not be repeated four times).90
isXC
,900
isCM
. - The fives characters can not be repeated.
10
is always represented asX
, never asVV
.100
is alwaysC
, neverLL
. - Roman numerals are read left to right, so the order of characters matters very much.
DC
is600
;CD
is a completely different number (400
, “100
less than500
”).CI
is101
;IC
is not even a valid Roman numeral (because you can’t subtract1
directly from100
; you would need to write it asXCIX
, “10
less than100
, then1
less than10
”).
Checking for thousands
What would it take to validate that an arbitrary string is a valid Roman numeral? Let’s take it one digit at a time. Since Roman numerals are always written highest to lowest, let’s start with the highest: the thousands place. For numbers 1000 and higher, the thousands are represented by a series of M characters.
Get hands-on with 1200+ tech skills courses.