Today, most people in the world use Arabic numerals (0β9). But if you travelled back two thousand years, you'd find that most Europeans were using Roman numerals instead.
To write a Roman numeral we use the following Latin letters, each of which has a value:
M | D | C | L | X | V | I |
---|---|---|---|---|---|---|
1000 | 500 | 100 | 50 | 10 | 5 | 1 |
A Roman numeral is a sequence of these letters, and its value is the sum of the letters' values.
For example, XVIII
has the value 18 (10 + 5 + 1 + 1 + 1 = 18
).
There's one rule that makes things trickier though, and that's that the same letter cannot be used more than three times in succession.
That means that we can't express numbers such as 4 with the seemingly natural IIII
.
Instead, for those numbers, we use a subtraction method between two letters.
So we think of 4
not as 1 + 1 + 1 + 1
but instead as 5 - 1
.
And slightly confusingly to our modern thinking, we write the smaller number first.
This applies only in the following cases: 4 (IV
), 9 (IX
), 40 (XL
), 90 (XC
), 400 (CD
) and 900 (CM
).
Order matters in Roman numerals! Letters (and the special compounds above) must be ordered by decreasing value from left to right.
Here are some examples:
105 => CV
---- => --
100 => C
+ 5 => V
106 => CVI
---- => --
100 => C
+ 5 => V
+ 1 => I
104 => CIV
---- => ---
100 => C
+ 4 => IV
And a final more complex example:
1996 => MCMXCVI
----- => -------
1000 => M
+ 900 => CM
+ 90 => XC
+ 5 => V
+ 1 => I
Your task is to convert a number from Arabic numerals to Roman numerals.
For this exercise, we are only concerned about traditional Roman numerals, in which the largest number is MMMCMXCIX (or 3,999).
There are lots of different ways to convert between Arabic and Roman numerals. We recommend taking a naive approach first to familiarise yourself with the concept of Roman numerals and then search for more efficient methods.
Make sure to check out our Deep Dive video at the end to explore the different approaches you can take!
This exercise focuses on adding methods to the Int
structure in Swift.
This is easiest done through using extensions.
extension Int {
//
}
The Int
documentation can be found here.
Sign up to Exercism to learn and master Swift with 35 concepts, 103 exercises, and real human mentoring, all for free.
Explore a variety of different solutions to this tricky exercise, ranging from recursion to table-based lookups, one solution that makes use of mixed radix numbers, and some simplistic cheating by Common Lisp.