Integer to Roman - Problem
Convert Integer to Roman Numerals
Roman numerals are an ancient number system that's still used today - from clock faces to movie sequels! Your task is to convert a modern integer into its ancient Roman numeral equivalent.
The Roman numeral system uses seven symbols:
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
Special Rules:
- Generally, symbols are combined from largest to smallest (left to right)
- When a smaller numeral appears before a larger one, it means subtraction
- Only specific subtractive combinations are allowed:
IV(4),IX(9),XL(40),XC(90),CD(400),CM(900) - You can't repeat a symbol more than 3 times consecutively
Examples: 3 → III, 4 → IV, 9 → IX, 58 → LVIII, 1994 → MCMXCIV
Input & Output
example_1.py — Basic Conversion
$
Input:
num = 3
›
Output:
"III"
💡 Note:
3 is represented as three I's in Roman numerals: I + I + I = III
example_2.py — Subtractive Case
$
Input:
num = 58
›
Output:
"LVIII"
💡 Note:
58 = 50 + 5 + 3 = L + V + III = LVIII
example_3.py — Complex Number
$
Input:
num = 1994
›
Output:
"MCMXCIV"
💡 Note:
1994 = 1000 + 900 + 90 + 4 = M + CM + XC + IV = MCMXCIV (uses multiple subtractive forms)
Constraints
- 1 ≤ num ≤ 3999
- Input is guaranteed to be a valid integer
- Note: Roman numerals don't have a symbol for zero
Visualization
Tap to expand
Understanding the Visualization
1
Prepare Coin Types
Set up Roman 'coins' including special subtractive combinations (IV=4, IX=9, etc.)
2
Start with Largest
Always try to use the largest possible Roman value first
3
Calculate Quantity
Use division to find how many of each 'coin' type to use
4
Update Remainder
Use modulo to find what's left after using current coin type
5
Repeat Process
Continue with next largest coin type until remainder is zero
Key Takeaway
🎯 Key Insight: Pre-computing all valid Roman combinations (including subtractive forms) and processing them in descending order allows for an elegant greedy algorithm that builds the optimal Roman numeral representation in a single pass.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code