### Convert an Integer to a Roman Numeral with Subtractive Rules
To convert an integer to a Roman numeral, especially accounting for subtractive combinations like IV (4), IX (9), XL (40), and so on, you can follow a structured approach using predefined mappings of integers to Roman numerals. This method ensures that the subtractive notation is applied correctly.
The core idea is to use two arrays: one for the integer values and another for their corresponding Roman numeral representations. These arrays are ordered from the largest to the smallest values, including the special subtractive cases. The algorithm iterates through these values, subtracting them from the input number while appending the corresponding Roman numeral symbols to the result string.
Here’s how this can be implemented in Python:
```python
def int_to_roman(num):
# Define the mapping of integers to Roman numerals, including subtractive cases
val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
syms = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
roman_numeral = ""
i = 0
while num > 0:
while num >= val[i]:
roman_numeral += syms[i]
num -= val[i]
i += 1
return roman_numeral
```
This function works by repeatedly subtracting the largest possible value from the given number and appending the corresponding Roman numeral symbol(s) to the result string. For example, when converting the number 1994, the function first subtracts 1000 (M), then 900 (CM), followed by 90 (XC), and finally 4 (IV), resulting in the Roman numeral "MCMXCIV" [^2].
### Explanation of Subtractive Notation Handling
Subtractive notation is used in specific cases where a smaller numeral precedes a larger one, indicating subtraction rather than addition. These cases include:
- IV for 4 (5 - 1)
- IX for 9 (10 - 1)
- XL for 40 (50 - 10)
- XC for 90 (100 - 10)
- CD for 400 (500 - 100)
- CM for 900 (1000 - 100)
By including these subtractive combinations in the `val` and `syms` arrays, the function ensures that the correct Roman numeral representation is generated without requiring additional logic to handle these cases separately [^3].
### Example Usage
For instance, if you call `int_to_roman(1994)`, the function will return `"MCMXCIV"`, which accurately represents the year 1994 in Roman numerals [^2].
Similarly, calling `int_to_roman(58)` will return `"LVIII"`, representing 58 as L (50), V (5), and III (3).