25 is written as XXV in Roman numerals.
How to convert an integer into a Roman numeral
Key takeaways:
Roman numerals use Latin letters to represent numbers, combining both additive and subtractive principles. Key values include
I (1),V (5),X (10),L (50),C (100),D (500), andM (1000), with combinations likeIV (4),IX (9), and others for subtraction.To convert an integer, iterate through a lookup table of Roman numerals from largest to smallest. Divide the number by each numeral’s value, append the corresponding symbol, and use modulus to update the number.
The Integer to Roman problem is a classic algorithmic problem that involves converting an integer into its corresponding Roman numeral representation.
What are Roman numerals?
Roman numerals, originating in ancient Rome, were widely used across Europe until the Late Middle Ages and are still in use today in various contexts. Each numeral corresponds to a specific value, represented by a combination of Latin letters. They are explained in the table below:
Letter | Value |
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
The number “3” is written as III in Roman numerals, just three ones added together. The number “12” is written as XII, simply X + II. The number 27 is written as XXVII, which is XX + V + II, and so on.
The system also incorporates subtraction principles, where certain combinations like “IV” represent 4 (5 - 1) and “IX” represent 9 (10 - 1). Similarly, there are combinations for numbers like 40 (XL) and 90 (XC), making the system more complex than a simple additive representation. This will make our table look like this:
Letter | Value |
I | 1 |
IV | 4 |
V | 5 |
IX | 9 |
X | 10 |
XL | 40 |
L | 50 |
XC | 90 |
C | 100 |
CD | 400 |
D | 500 |
CM | 900 |
M | 1000 |
The conversion algorithm
We can convert an integer to a Roman numeral by combining division and modulus. Let’s explain the algorithm with the example of the number “1400”.
Firstly, we’ll iterate through our lookup table from the largest number to the smallest number.
We’ll check how many times the current Roman numeral can fit into the given number. We will do this by dividing the number with the value of the Roman numeral from the lookup table. The result of the division will give us the quotient, which will be the number of times that particular numeral will be present in the final result.
We multiply the division result with the Roman numeral and append the result to a string that will carry our final output.
Finally, we will take the modulus of the number with the corresponding Roman numeral, subtracting the largest possible unit from the number and comparing the remainder to the rest of the table. We will continue this until the remainder is 0.
Converting an integer into a Roman numeral in Python
Let’s turn this algorithm into a Python code:
def intToRoman(num):LookupTable = [["I",1], ["IV",4], ["V",5], ["IX",9],["X",10], ["XL",40], ["L",50], ["XC",90], ["C",100], ["CD",400], ["D",500],["CM",900], ["M",1000]]result = ""for symbol, value in reversed(LookupTable):if num // value:count = num//valueresult = result + (symbol * count)num = num % valuereturn resultprint(intToRoman(2467))
Let’s explain the code line by line:
Lines 2–4: Here, we initialize a list of lists called
LookupTable. This is a list of lists where each inner list contains a Roman numeral symbol and its corresponding integer value.Line 6: Here, we initialize the
resultvariable, which will store our resultant string.Line 7: Here, we initiate a loop that will iterate through the
LookupTable. We iterate in reverse order, as it is important to start from the largest number.Lines 8–11: We check if the
valuefrom theLookupTablecan divide the thenumevenly. If it can, then we store the result in thecountvariable to see how many times that particular symbol will appear in theresultstring. We then multiply thesymbolwe got from theLookupTableand multiply with thecountvariable to and add append it to theresultvariable. Finally, we update thenumvariable by taking the modulus of it with thevaluevariable.
Conclusion
Converting an integer to a Roman numeral is a straightforward process when utilizing a structured lookup table and basic division and modulus operations. By iterating from the largest Roman numeral value downwards, you can efficiently build the corresponding numeral representation. This method ensures that both additive and subtractive Roman numeral principles are respected, providing an accurate and optimized conversion.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
How would you write 25 as a Roman numeral?
What is XXXIII in Roman numerals?
What is 0 in Roman numerals?
Free Resources