Editor

Integer to Roman Numerals

#68 · Strings · Intermediate · 65 XP · about 10 min

Roman numerals use seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Symbols are written from largest to smallest and their values are added, except for six pairs where a smaller symbol in front of a larger one is subtracted: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400 and CM = 900. No symbol appears more than three times in a row.

Write to_roman(n) that returns the Roman numeral for the whole number n as a string.

Examples

to_roman(1987)  # returns 'MCMLXXXVII'
to_roman(58)    # returns 'LVIII'
to_roman(4)     # returns 'IV'

1987 is M (1000) + CM (900) + LXXX (80) + VII (7).

Constraints

  • 1 <= n <= 3999

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.