Integer to Roman Problem and Solution

1. Introduction

The "Integer to Roman" problem involves converting an integer to a Roman numeral. Roman numerals are represented by combinations of letters from the Latin alphabet, mainly including I, V, X, L, C, D, and M. The goal is to write a function that converts an integer to its Roman numeral equivalent.

2. Solution in C++

#include <iostream>
#include <vector>
using namespace std;

string intToRoman(int num) {
    vector<pair<int, string>> values = {{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
                                        {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"},
                                        {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}};
    string roman;
    for (auto &[value, symbol] : values) {
        while (num >= value) {
            num -= value;
            roman += symbol;
        }
    }
    return roman;
}

// Example usage
int main() {
    int num = 58;
    cout << "Roman: " << intToRoman(num) << endl;
    return 0;
}

Output:

Roman: LVIII

Explanation:

1. intToRoman in C++ uses a vector of pairs to map integers to Roman numerals.

2. It iteratively subtracts the largest possible value from num while appending the corresponding Roman numeral.

3. The process continues until num is reduced to zero.

3. Solution in Java

public class IntegerToRoman {
    public static String intToRoman(int num) {
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

        StringBuilder roman = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            while (num >= values[i]) {
                num -= values[i];
                roman.append(symbols[i]);
            }
        }
        return roman.toString();
    }

    // Example usage
    public static void main(String[] args) {
        int num = 58;
        System.out.println("Roman: " + intToRoman(num));
    }
}

Output:

Roman: LVIII

Explanation:

1. Java's intToRoman method uses arrays to store the Roman numeral mappings.

2. It constructs the Roman numeral by iterating over the arrays, subtracting and appending symbols.

3. The loop runs until the input number is fully converted.

4. Solution in Python

def int_to_roman(num):
    val = [
        (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"),
        (100, "C"), (90, "XC"), (50, "L"), (40, "XL"),
        (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I")
    ]
    roman = ""
    for i, r in val:
        while num >= i:
            num -= i
            roman += r
    return roman

# Example usage
num = 58
print("Roman:", int_to_roman(num))

Output:

Roman: LVIII

Explanation:

1. Python's int_to_roman uses a list of tuples for the numeral mappings.

2. It iterates over the list, subtracting values and appending Roman numerals to the result string.

3. Continues until the entire number is converted to Roman numeral format.


Comments