|
1 | 1 | package com.thealgorithms.conversions;
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * Converting Integers into Roman Numerals |
| 4 | + * A utility class to convert integers into Roman numerals. |
5 | 5 | *
|
6 |
| - * <p> |
7 |
| - * ('I', 1); ('IV',4); ('V', 5); ('IX',9); ('X', 10); ('XL',40); ('L', 50); |
8 |
| - * ('XC',90); ('C', 100); ('D', 500); ('M', 1000); |
| 6 | + * <p>Roman numerals follow these rules: |
| 7 | + * <ul> |
| 8 | + * <li>I = 1</li> |
| 9 | + * <li>IV = 4</li> |
| 10 | + * <li>V = 5</li> |
| 11 | + * <li>IX = 9</li> |
| 12 | + * <li>X = 10</li> |
| 13 | + * <li>XL = 40</li> |
| 14 | + * <li>L = 50</li> |
| 15 | + * <li>XC = 90</li> |
| 16 | + * <li>C = 100</li> |
| 17 | + * <li>D = 500</li> |
| 18 | + * <li>M = 1000</li> |
| 19 | + * </ul> |
| 20 | + * |
| 21 | + * <p>Conversion is based on repeatedly subtracting the largest possible Roman numeral value |
| 22 | + * from the input number until it reaches zero. For example, 1994 is converted as: |
| 23 | + * <pre> |
| 24 | + * 1994 -> MCMXCIV (1000 + 900 + 90 + 4) |
| 25 | + * </pre> |
9 | 26 | */
|
10 | 27 | public final class IntegerToRoman {
|
| 28 | + |
| 29 | + // Array of Roman numeral values in descending order |
| 30 | + private static final int[] ALL_ROMAN_NUMBERS_IN_ARABIC = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; |
| 31 | + |
| 32 | + // Corresponding Roman numeral symbols |
| 33 | + private static final String[] ALL_ROMAN_NUMBERS = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; |
| 34 | + |
11 | 35 | private IntegerToRoman() {
|
12 | 36 | }
|
13 | 37 |
|
14 |
| - private static final int[] ALL_ROMAN_NUMBERS_IN_ARABIC = new int[] { |
15 |
| - 1000, |
16 |
| - 900, |
17 |
| - 500, |
18 |
| - 400, |
19 |
| - 100, |
20 |
| - 90, |
21 |
| - 50, |
22 |
| - 40, |
23 |
| - 10, |
24 |
| - 9, |
25 |
| - 5, |
26 |
| - 4, |
27 |
| - 1, |
28 |
| - }; |
29 |
| - private static final String[] ALL_ROMAN_NUMBERS = new String[] { |
30 |
| - "M", |
31 |
| - "CM", |
32 |
| - "D", |
33 |
| - "CD", |
34 |
| - "C", |
35 |
| - "XC", |
36 |
| - "L", |
37 |
| - "XL", |
38 |
| - "X", |
39 |
| - "IX", |
40 |
| - "V", |
41 |
| - "IV", |
42 |
| - "I", |
43 |
| - }; |
44 |
| - |
45 |
| - // Value must be > 0 |
| 38 | + /** |
| 39 | + * Converts an integer to its Roman numeral representation. |
| 40 | + * Steps: |
| 41 | + * <ol> |
| 42 | + * <li>Iterate over the Roman numeral values in descending order</li> |
| 43 | + * <li>Calculate how many times a numeral fits</li> |
| 44 | + * <li>Append the corresponding symbol</li> |
| 45 | + * <li>Subtract the value from the number</li> |
| 46 | + * <li>Repeat until the number is zero</li> |
| 47 | + * <li>Return the Roman numeral representation</li> |
| 48 | + * </ol> |
| 49 | + * |
| 50 | + * @param num the integer value to convert (must be greater than 0) |
| 51 | + * @return the Roman numeral representation of the input integer |
| 52 | + * or an empty string if the input is non-positive |
| 53 | + */ |
46 | 54 | public static String integerToRoman(int num) {
|
47 | 55 | if (num <= 0) {
|
48 | 56 | return "";
|
49 | 57 | }
|
50 | 58 |
|
51 | 59 | StringBuilder builder = new StringBuilder();
|
52 |
| - |
53 |
| - for (int a = 0; a < ALL_ROMAN_NUMBERS_IN_ARABIC.length; a++) { |
54 |
| - int times = num / ALL_ROMAN_NUMBERS_IN_ARABIC[a]; |
55 |
| - for (int b = 0; b < times; b++) { |
56 |
| - builder.append(ALL_ROMAN_NUMBERS[a]); |
57 |
| - } |
58 |
| - |
59 |
| - num -= times * ALL_ROMAN_NUMBERS_IN_ARABIC[a]; |
| 60 | + for (int i = 0; i < ALL_ROMAN_NUMBERS_IN_ARABIC.length; i++) { |
| 61 | + int times = num / ALL_ROMAN_NUMBERS_IN_ARABIC[i]; |
| 62 | + builder.append(ALL_ROMAN_NUMBERS[i].repeat(Math.max(0, times))); |
| 63 | + num -= times * ALL_ROMAN_NUMBERS_IN_ARABIC[i]; |
60 | 64 | }
|
61 | 65 |
|
62 | 66 | return builder.toString();
|
63 | 67 | }
|
64 |
| - |
65 |
| - public static void main(String[] args) { |
66 |
| - System.out.println(IntegerToRoman.integerToRoman(2131)); |
67 |
| - } |
68 | 68 | }
|
0 commit comments