An algorithm converting a natural number in its decimal representation into a roman number is given in the following:

Algorithm: Converting Decimal Numbers to Roman Numbers

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

usage

x = 751 r = dec2roman(x) print(str(x) + " = " + r)

  1. will output
  2. will output

Thank you to the contributors under CC BY-SA 4.0!

Github:
bookofproofs


References

Bibliography

  1. Hermann, D.: "Algorithmen Arbeitsbuch", Addison-Wesley Publishing Company, 1992