/kata-roman-numerals-clojure

TDD exercise (Clojure)

Primary LanguageClojureMIT LicenseMIT

Roman Numerals kata

Problem description: Convert a number to Roman Numerals representation

Following is the excerpt from Wiklipedia article https://en.wikipedia.org/wiki/Roman_numerals.

Numbers in Roman Numerals system are represented by combinations of letters from the Latin alphabet. Roman numerals, as used today, are based on seven symbols:

  • I : 1
  • V : 5
  • X : 10
  • L : 50
  • C : 100
  • D : 500
  • M : 1000

Basic decimal pattern

The numbers from 1 to 10 are expressed in Roman numerals as follows:

I, II, III, IV, V, VI, VII, VIII, IX, X.

The numeral for 4 is represented with IV (one less than 5), instead of IIII; and the numeral for 9 (VIIII) is represented with IX (one less than 10) instead of VIIII. This feature of Roman numerals is called subtractive notation.

The system being basically decimal, tens and hundreds follow the same pattern: 10 to 100 are counted in tens, with X taking the place of I, L taking the place of V and C taking the place of X:

X, XX, XXX, XL, L, LX, LXX, LXXX, XC, C.

Note that 40 (XL) and 90 (XC) follow the same subtractive pattern as 4 and 9.

Similarly, 100 to 1000 (counting in hundreds):

C, CC, CCC, CD, D, DC, DCC, DCCC, CM, M.

Again - 400 (CD) and 900 (CM) follow the standard subtractive pattern.

In the Roman numeral system being basically decimal, each "place" is added in descending sequence from left to right, as with Arabic numerals. For example:

39 = "Thirty nine" (XXX+IX) = XXXIX.
246 = "Two hundred and forty six" (CC+XL+VI) = CCXLVI.

As each place has its own notation there is no need for place keeping zeros, so "missing places" are ignored, as in Latin (and English) speech, thus:

207 = "Two hundred and seven" (CC+VII) = CCVII
1066 = "A thousand and sixty six" (M+LX+VI) = MLXVI.

Roman numerals for large numbers are nowadays seen mainly in the form of year numbers, as in these examples:

1776 (M+DCC+LXX+VI) = MDCCLXXVI
1954 (M+CM+L+IV) = MCMLIV
1990 (M+CM+XC) = MCMXC
2014 (MM+X+IV) = MMXIV
2018 = MMXVIII