An algorithm converting a natural number in its decimal representation into a roman number is given in the following:
def getDigit(x, e, f, z): digit = "" if x == 9: digit += e + z elif x > 4: digit += f for k in range(x, 5, -1): digit += e else: if x == 4: digit += e + f else: for k in range(x, 0, -1): digit += e return digit
def dec2roman(x): ''' convert decimal number into roman number ''' r = "" while x > 999: x -= 1000 r += "M" r += getDigit(x // 100, 'C', 'D', 'M') r += getDigit(x // 10 - 10*(x // 100), 'X', 'L', 'C') r += getDigit(x % 10, "I", "V", "X") return r
x = 751 r = dec2roman(x) print(str(x) + " = " + r)