Roman numerals are numbers used by the ancient Romans in their non-positional number system .
| Indo-Arab | |
|---|---|
| Arab Tamil Burmese | Khmer Lao Mongolian Thai |
| East Asian | |
| Chinese Japanese Suzhou Korean | Vietnamese Counting sticks |
| Alphabetic | |
| Abjadiya Armenian Ariabhata Cyrillic Greek | Georgian Ethiopian Jewish Akshara Sankhya |
| Other | |
| Babylonian Egyptian Etruscan Roman Danube | Attic Kipu Mayan Aegean KPPU Symbols |
| 2 , 3 , 4 , 5 , 6 , 8 , 10 , 12 , 16 , 20 , 60 | |
| Negative Position | |
| Symmetrical | |
| Fibonacci | |
| Single (unary) | |
| This page or section contains special Unicode characters . If you do not have the required fonts , some characters may not display correctly. |
Natural numbers are written by repeating these numbers. At the same time, if a larger digit is before a smaller one, then they add up (the principle of addition), but if a smaller one is before a larger one, the smaller one is subtracted from the larger one (subtraction principle). The last rule applies only to avoid a four-fold repetition of the same digit.
Roman numerals appeared 500 years BC from the Etruscans (see the Etruscan alphabet ), which could borrow some of the numbers from the Proto-Celts .
Roman Numerals
Roman numerals are now better known than any other ancient number system. This is explained not so much by some special merits of the Roman system, but by the enormous influence enjoyed by the Roman Empire in the relatively recent past. Etruscans who conquered Rome in the 7th century BC e., experienced the influence of the Eastern Mediterranean cultures. This partly explains the similarity of the basic principles of the Roman and Attic number systems. Both systems were decimal, although the number five played a special role in both number systems. Both systems used repeated characters to record numbers.
The old Roman characters for the numbers 1, 5, 10, 100 and 1000 were, respectively, the characters I, V, X, Θ (or ⊕ , or ⊗ ) and Φ (or ↀ , or CIƆ ). Although much has been written about the original meaning of these symbols, we still do not have a satisfactory explanation for them. According to one popular theory, the Roman numeral V represents an open hand with four fingers pressed together and with the thumb set apart; the symbol X, according to the same theory, depicts two crossed arms or a double digit V. The symbols of the numbers 100 and 1000 may originate from the Greek letters Θ and φ. It is not known whether the later designations C and M originated from old Roman characters or whether they are acrophonically related to the initial letters of the Latin words meaning 100 (centum) and 1000 (millet). It is believed that the Roman symbol of the number 500, the letter D , originated from half of the old symbol denoting 1000. Unless we assume that most of the Roman symbols were most likely not acrophonic and that the intermediate symbols for 50 and 500 were not combinations of the symbols of the numbers 5 and 10 or 5 and 100, then the rest of the Roman numeral system resembled the Attic. Of course, in the details they differed. The Romans often used the principle of subtraction, so sometimes instead of VIIII they used IX, and XC instead of LXXXX; relatively later symbol IV instead of IIII.
In general, the Romans were not inclined to do mathematics, so they did not feel a special need for large numbers. Nevertheless, they occasionally used the symbol CCIƆƆ to designate 10000, and the symbol CCCIƆƆƆ for the number 100000. The halves of these symbols were sometimes used to indicate the numbers 5000 ( IƆƆ ) and 50,000 ( IƆƆƆ ).
The Romans avoided shots as stubbornly as large numbers. In practical tasks related to measurements, they did not use fractions, dividing the unit of measurement usually into 12 parts, so that the measurement result is presented as a composite number, the sum of multiple different units, as is done today, when the length is expressed in yards, feet and inches. The English words "ounce" ( ounce ) and "inch" ( inch ) come from the Latin word lat. uncia ( ounce ) denoting one twelfth of the basic unit of length. [1] [2]
| one | I | lat unus, unum |
| five | V | lat quinque |
| ten | X | lat decem |
| 50 | L | lat quinquaginta |
| 100 | C | lat centum |
| 500 | D | lat quingenti |
| 1000 | M | lat mille |
| Number | Designation |
|---|---|
| one | I |
| 2 | II |
| 3 | III |
| four | IV (before the XIX century it was IIII) |
| five | V |
| 6 | VI |
| 7 | VII |
| eight | VIII |
| 9 | IX |
| ten | X |
| eleven | Xi |
| 20 | XX |
| thirty | XXX |
| 40 | XL |
| 50 | L |
| 60 | Lx |
| 70 | Lxx |
| 80 | Lxxx |
| 90 | Xc |
| 100 | C |
| 200 | CC |
| 300 | CCC |
| 400 | CD |
| 500 | D; IƆ |
| 600 | DC IƆC |
| 700 | DCC IƆCC |
| 800 | DCCC; IƆCCC |
| 900 | CM CCIƆ |
| 1000 | M; ↀ; CiƆ |
| 2000 | MM CIƆCIƆ |
| 3000 | MMM CIƆCIƆCIƆ |
| 3999 | MMMCMXCIX |
| 4000 | M V ; ↀↁ; CiƆiƆƆ |
| 5000 | V ; ↁ; IƆƆ |
| 6000 | V M; ↁↀ; IƆƆCIƆ |
| 7000 | V MM; ↁↀↀ; IƆƆCIƆCIƆ |
| 8000 | V MMM; ↁↀↀↀ; IƆƆCIƆCIƆCIƆ |
| 9000 | M X ↀↂ; CIƆCCIƆƆ |
| 10,000 | X ; ↂ; CCIƆƆ |
| 20,000 | XX ; ↂↂ; CCIƆƆCCIƆƆ |
| 30,000 | XXX ↂↂↂ; CCIƆƆCCIƆƆCCIƆƆ |
| 40,000 | Xl ; ↂↇ; CCIƆƆIƆƆƆ |
| 50,000 | L ; ↇ; IƆƆƆ |
| 60,000 | Lx ; ↇↂ; IƆƆƆCCIƆƆ |
| 70,000 | LXX ; ↇↂↂ; IƆƆƆCCIƆƆCCIƆƆ |
| 80,000 | LXXX ; ↇↂↂↂ; IƆƆƆCCIƆƆCCIƆƆCCIƆƆ |
| 90,000 | XC ; ↂↈ; CCIƆƆCCCIƆƆƆ |
| 100,000 | C ; ↈ; CCCIƆƆƆ |
| 200,000 | CC ; ↈↈ; CCCIƆƆƆCCCIƆƆƆ |
| 300,000 | CCC ; ↈↈↈ; CCCIƆƆƆCCCIƆƆƆCCCIƆƆƆ |
| 400,000 | CD CCCIƆƆƆIƆƆƆƆ |
| 500,000 | D ; IƆƆƆƆ |
| 600,000 | DC IƆƆƆƆCCCIƆƆƆ |
| 700,000 | DCC IƆƆƆƆCCCIƆƆƆCCCIƆƆƆ |
| 800,000 | DCCC |
| 900,000 | CM |
| 1,000,000 | M |
| 2,000,000 | MM |
| 3,000,000 | MMM |
| 4,000,000 | M v |
| 5,000,000 | V |
| 6,000,000 | V m |
| 7,000,000 | V MM |
| 8,000,000 | V MMM |
| 9,000,000 | M X |
To correctly record large numbers in Roman numerals, you must first write down the number of thousands, then hundreds, then tens and, finally, units.
There is no zero in the Roman numeral system, but previously used the notation of zero as nulla (no), nihil (nothing) and N (the first letter of these words).
Moreover, some of the numbers (I, X, C, M) can be repeated, but no more than three times in a row ; thus, with their help, you can write any integer no more than 3999 (MMMCMXCIX). In the early periods there were signs to indicate larger numbers - 5,000, 10,000, 50,000 and 100,000 (then the maximum number under the mentioned rule is 399,999). When writing numbers in the Roman numeral system, a smaller digit may be to the right of the larger one; in this case she is added to her. For example, the number 283 in Roman is written as CCLXXXIII, i.e. 100 + 100 + 50 + 30 + 3 = 283. Here, the figure depicting a hundred is repeated twice, and the figures depicting a ten and one respectively are repeated three times.
Example: the number 1988. One thousand M, nine hundred CM, eight tens LXXX, eight units VIII. We write them together: MCMLXXXVIII.
Quite often, to highlight numbers in the text, a line was drawn above them: LXIV . Sometimes a line was drawn both above and below: XXXII - in particular, it is customary to highlight Roman numerals in Russian handwritten text (this is not used in the typographic set due to technical complexity). For other authors, the bar above could indicate an increase in the value of the figure by a factor of 1000: V = 5000.
Everywhere, the number “four” was written down as “IV” only in the 19th century ; before that, the record “IIII” was most often used. However, the record “IV” can already be found in the documents of the manuscript “ Forme of Cury ” dating back to 1390 . On the watch dials , in most cases, “IIII” is traditionally used instead of “IV”, mainly for aesthetic reasons: this spelling provides visual symmetry with the numbers “VIII” on the opposite side, and the inverted “IV” is more difficult to read than “IIII”. There is also a version that IV was not written on the dial because IV is the first letters of the name of the god Jupiter (IVPITER).
The smaller number can be written to the left of the larger one, then it should be subtracted from the larger one. In this case, only numbers denoting 1 or degree 10 can be subtracted, and only the two digits closest to the number to be subtracted (that is, subtracted multiplied by 5 or 10) can be used as the decrement. Repetition of a lower digit is not allowed. Thus, there are only six uses for the “subtraction rule”:
- Iv = 4
- IX = 9
- XL = 40
- XC = 90
- CD = 400
- CM = 900
For example, the number 94 will be XCIV = 100 - 10 + 5 - 1 = 94 - the so-called "rule of subtraction" (appeared in the era of late antiquity, and before that the Romans wrote the number 4 as IIII, and the number 40 as XXXX).
It should be noted that other methods of "subtraction" are unacceptable; so, the number 99 should be written as XCIX, but not as IC. However, today in some cases a simplified notation of Roman numbers is also used: for example, in Microsoft Excel, when converting Arabic numbers to Roman using the “ROMAN ()” function, you can use several types of representation of numbers, from classical to very simplified (so, 499 can be written as CDXCIX, LDVLIV, XDIX, VDIV or ID). The simplification is that to reduce any digit to the left of it, any other digit can be written:
- 999. Thousand (M), subtract 1 (I), we get 999 (IM) instead of CMXCIX. Corollary: 1999 - MIM instead of MCMXCIX
- 95. One hundred (C), subtract 5 (V), we get 95 (VC) instead of XCV
- 1950: Thousand (M), subtract 50 (L), we get 950 (LM). Corollary: 1950 - MLM instead of MCML
Cases of such a recording of numbers (usually years) are often found in the credits of US television series. For example, for the year 1998: IIMM instead of MCMXCVIII.
Using Roman numerals, you can also write large numbers. To do this, a line is put above the numbers that mean thousands, and a double line above the numbers that mean millions. For example, the number 123123 would look like this:
CXXIII CXXIII
A million is like I , but not only with one, but with two traits at the head: I
Application
In Russian, Roman numerals are used in the following cases:
- Century or Millennium Number: XIX Century, II Millennium BC e.
- Verb conjugations
- The serial number of the monarch.
- The number of the corps in the Armed Forces.
- Blood type on the stripes of the uniform of the Armed Forces of the Russian Federation.
- Volume number in a multi-volume book (sometimes - part numbers of a book, sections or chapters ).
- In some publications - numbers of sheets with a preface to the book, so as not to correct the links inside the main text when changing the preface.
- Marking the dials of watches "antique" .
- Other important events or items on the list, for example: V postulate of Euclid , II World War , XX Congress of the CPSU , Games of the XXII Olympiad and the like.
- The valency of chemical elements.
- The serial number of the steps in the scale.
- Base homeopathic dilutions (Conium X3, Aconitum C200 and the like).
- In mathematical analysis , the number of the derivative is written in Roman numerals, however, when reading (usually), it says “stroke” instead of I, “two strokes” instead of II, “three strokes” instead of III. Finally, starting with IV, the “fourth derivative” is read: but .
Roman numerals were widely used in the USSR when indicating the date for the month of the year, for example: 11 / III-85 or 9.XI.89, this can be seen on many archival documents of those times. Similarly, through a slash , including recorded the date of the lesson in class magazines , for example: 24 / II. To indicate the dates of life and death on tombstones, a special format was often used, where the month of the year was also indicated by Roman numerals, for example: . A similar format in the 1970-1980s was used in medical certificates.
With the transition to computer processing of information , date formats based on Roman numerals have virtually disappeared.
In other languages, the scope of Roman numerals may have features. In Western countries, Roman numerals often record the number of the year, for example, on the gables of buildings and in the credits of film and video production.
In modern Lithuania, the road signs , shop windows , and company signs in Roman numerals can indicate the days of the week .
Unicode
The Unicode standard recommends using regular Latin letters to represent Roman numerals. [3] However, the standard also includes special characters for Roman numerals as part of the Number Forms [4] in the area of characters with codes U + 2160 through U + 2188. For example, MCMLXXXVIII may be represented in the form ⅯⅭⅯⅬⅩⅩⅩⅧ. This range includes both lowercase and uppercase digits for writing numbers from 1 (Ⅰ or I) to 12 (Ⅻ or XII), including combined glyphs for compound numbers such as 8 (Ⅷ or VIII), mainly for ensure compatibility with East Asian character sets in industry standards such as JIS X 0213 where these characters are defined. Combined glyphs are used to represent numbers that were previously made up of individual characters (for example, Ⅻ instead of representing it as Ⅹ and Ⅱ). In addition to this, glyphs exist for archaic [4] forms of notation 1000, 5000, 10 000, large inverse C (Ɔ), late notation 6 (ↅ, similar to Greek stigma : Ϛ), early notation for 50 ( ↆ, similar to the arrow pointing down ↓ ⫝⊥ [5] ), 50,000, and 100,000. It should be noted that the small inverse of c, ↄ is not included in Roman numerals, but is included in the Unicode standard as the uppercase clavdian letter Ↄ.
| Code | 0 | one | 2 | 3 | four | five | 6 | 7 | eight | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Value [6] | one | 2 | 3 | four | five | 6 | 7 | eight | 9 | ten | eleven | 12 | 50 | 100 | 500 | 1,000 |
| U + 2160 | Ⅰ 2160 | Ⅱ 2161 | Ⅲ 2162 | Ⅳ 2163 | Ⅴ 2164 | Ⅵ 2165 | Ⅶ 2166 | Ⅷ 2167 | Ⅸ 2168 | Ⅹ 2169 | Ⅺ 216A | Ⅻ 216B | Ⅼ 216C | Ⅽ 216D | Ⅾ 216E | Ⅿ 216F |
| U + 2170 | Ⅰ 2170 | Ⅱ 2171 | Ⅲ 2172 | Ⅳ 2173 | Ⅴ 2174 | Ⅵ 2175 | Ⅶ 2176 | Ⅷ 2177 | Ⅸ 2178 | Ⅹ 2179 | Ⅺ 217A | Ⅻ 217B | Ⅼ 217C | Ⅽ 217D | Ⅾ 217E | Ⅿ 217F |
| Value | 1,000 | 5,000 | 10,000 | 100 | 6 | 50 | 50,000 | 100,000 | ||||||||
| U + 2180 | ↀ 2180 | ↁ 2181 | ↂ 2182 | Ↄ 2183 | ↅ 2185 | ↆ 2186 | ↇ 2187 | ↈ 2188 | ||||||||
Displaying all these characters requires software that supports the Unicode standard and a font containing glyphs corresponding to these characters (for example, the Universalia font ).
Regular Expressions
The regular expression for checking Roman numerals is ^(M{0,3})(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$ [7] In Perl, you can use the regular expression m/\b((?:M{0,3}?(?:D?C{0,3}|C[DM])?(?:L?X{0,3}|X[LC])?(?:I{0,3}?V?I{0,3}|I[VX])))\b/gs ^(M{0,3})(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$ to search for Roman numerals in a string m/\b((?:M{0,3}?(?:D?C{0,3}|C[DM])?(?:L?X{0,3}|X[LC])?(?:I{0,3}?V?I{0,3}|I[VX])))\b/gs .
Conversion
To convert numbers written in Arabic numerals to Roman, special functions are used.
For example, in the English version of Microsoft Excel and in any version of OpenOffice.org Calc , the ROMAN function (argument; form) exists for this; in the Russian version of Microsoft Excel, this function is called ROMAN (number; form). The optional argument "form" can take values from 0 to 4, as well as "False" and "Truth." The absence of the argument “Form” or the equality of its 0 or “Truth” gives a “classical” (strict) form of transformation; meaning 4 or “False” gives the most simplified; values 1, 2, 3 give intermediate in terms of severity-simplification options. Differences appear, for example, at the numbers 45, 49, 495, 499 (the former are indicated in the range [1; 3999]).
| the form | 0 | one | 2 | 3 | four | |
| number | ||||||
| 45 | Xlv | Vl | Vl | Vl | Vl | |
| 49 | Xlix | Vliv | IL | IL | IL | |
| 495 | CDXCV | LDVL | Xdv | Vd | Vd | |
| 499 | CDXCIX | LDVLIV | Xdix | Vdiv | ID |
For non-integer values of the argument "number", rounding down to the whole is performed; if after that the value is greater than 3999 or less than 0, then the function returns "#Value"; for value 0, an empty cell is returned.
var arab = [ 1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 ];
var roman = [ 'I' , 'IV' , 'V' , 'IX' , 'X' , 'XL' , 'L' , 'XC' , 'C' , 'CD' , 'D' , 'CM ' , ' M ' ];
function arabToRoman ( number )
{
if ( ! number ) return '' ;
var ret = '' ;
var i = arab . length - 1 ;
while ( number > 0 )
{
if ( number > = arab [ i ])
{
ret + = roman [ i ];
number - = arab [ i ];
}
else
{
i - ;
}
}
return ret ;
}
function romanToArab ( str )
{
str = str . toUpperCase ();
var ret = 0 ;
var i = arab . length - 1 ;
var pos = 0 ;
while ( i > = 0 && pos < str . length )
{
if ( str . substr ( pos , roman [ i ]. length ) == roman [ i ])
{
ret + = arab [ i ];
pos + = roman [ i ]. length ;
}
else
{
i - ;
}
}
return ret ;
}
#include <string.h>
const int arabar [] = { 1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 };
const char * romanar [] = { "I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D " , " CM " , " M " };
char * arab2roman ( unsigned short int arab ) {
static char roman [ 80 ];
const int m = sizeof ( arabar ) / sizeof ( int ) - 1 , arabmax = arabar [ m ];
const char romanmax = romanar [ m ] [ 0 ];
int i , n ;
if ( ! arab ) {
* roman = 0 ;
return roman ;
}
i = 0 ;
while ( arab > arabmax ) {
roman [ i ++ ] = romanmax ;
arab - = arabmax ;
}
n is m ;
while ( arab > 0 ) {
if ( arab > = arabar [ n ]) {
roman [ i ++ ] = romanar [ n ] [ 0 ];
if ( n & 1 )
roman [ i ++ ] = romanar [ n ] [ 1 ];
arab - = arabar [ n ];
} else
n is ;
}
roman [ i ] = 0 ;
return roman ;
}
unsigned short int roman2arab ( char * roman ) {
const int m = sizeof ( arabar ) / sizeof ( int ) - 1 ;
unsigned short int arab ;
int len , n , i , pir ;
len = strlen ( roman );
arab = 0 ;
n is m ;
i = 0 ;
while ( n > = 0 && i < len ) {
pir = n & 1 ;
if ( roman [ i ] == romanar [ n ] [ 0 ] && ( ! pir || roman [ i + 1 ] == romanar [ n ] [ 1 ])) {
arab + = arabar [ n ];
i + = 1 + pir ;
} else
n is ;
}
return arab ;
}
type str2 = string [ 2 ];
const
Rims : array [ 1..14 ] of str2 = ( 'M' , 'CM' , 'D' , 'CD' , 'C' , 'XC' , 'L' , 'XL' , 'X' , ' IX ' , ' V ' , ' IV ' , ' I ' , ' ' );
Arab : array [ 1..14 ] of integer = ( 1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1 , 0 );
var
N , NI , I , J : integer ;
S : string ;
function Arab2Rim ( N : integer ) : string ;
var S : string ;
I : integer ;
begin
S : = '' ; I : = 1 ;
while N > 0 do begin
while Arab [ I ] <= N do begin
S : = S + Rims [ I ];
N : = N - Arab [ I ]
end ;
I : = I + 1
end ;
Arab2Rim : = S
end ;
function Rim2Arab ( S : string ) : integer ;
var I , N : integer ;
begin
I : = 1 ; N : = 0 ;
while S <> '' do begin
while Rims [ I ] = Copy ( S , 1 , Length ( Rims [ I ]) ) do begin
S : = Copy ( S , 1 + Length ( Rims [ I ]), 255 );
N : = N + Arab [ I ]
end ;
I : = I + 1
end ;
Rim2Arab : = N
end ;
begin
WriteLn ( 'Translation from Arabic numerals to Roman. 1999 B_SA' );
{ Write ( 'Enter the number to convert:' ); ReadLn ( N );}
for NI : = 26 to 46 do
WriteLn ( NI , '=' , Arab2Rim ( NI ), 'back' , Rim2Arab ( Arab2Rim ( NI ) ));
end .
function Arab2Roman ( arab : integer ) : string ;
var
i : integer ;
d : integer ;
arab_str : string ;
arab_len : integer ;
begin
Result : = '' ;
arab_str : = IntToStr ( arab ) ;
arab_len : = Length ( arab_str ) ;
for i : = 0 to arab_len - 1 do begin
d : = StrToInt ( String ( arab_str [ arab_len - i ])) ;
if ( d + 1 ) mod 5 = 0 then
Result : = Copy ( 'IXCM' , 1 + i , 1 ) + Copy ( 'VXLCDM' , i * 2 + ( d + 1 ) div 5 , 1 ) + Result
else
Result : = Copy ( 'VLD' , 1 + i , d div 5 ) + Copy ( 'IIIXXXCCCMMM' , 1 + i * 3 , ( d mod 5 )) + Result ;
end ;
end ;
ten INPUT "ARAB NUMBER:" ; A $
20 FOR I = 0 TO LEN ( A $ ) -1
thirty X = VAL ( MID $ ( A $ , LEN ( A $ ) - I , 1 ))
40 IF X = 4 OR X = 9 THEN B $ = MID $ ( "IXCM" , I + 1 , 1 ) + MID $ ( "VXLCDM" , I * 2 + ( X + 1 ) / 5 , 1 ) + B $
50 IF X < 4 THEN B $ = MID $ ( "IIIXXXCCCMMM" , 1 + I * 3 , X ) + B $
ELSE IF X > 4 AND X < 9 THEN B $ = MID $ ( "VLD" , I + 1 , 1 ) + MID $ ( "IIIXXXCCCMMM" , 1 + I * 3 , X -5 ) + B $
60 NEXT I
70 PRINT "ROMAN NUMBER:" ; B $
string-join (
for $ num in (1999)
return (
('', 'M', 'MM', 'MMM') [($ num idiv 1000) mod 10 + 1],
('', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM') [($ num idiv 100) mod 10+ one],
('', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC') [($ num idiv 10) mod 10+ one],
('', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX') [$ num mod 10 + 1]
),
'')
use strict ;
use warnings ;
my $ n = 1999 ;
my $ nums = [
[ '' , qw (I II III IV V VI VI VII VIII IX) ],
[ '' , qw (X XX XXX XL L LX LXX LXXX XC) ],
[ '' , qw (C CC CCC CD D DC DCC DCCC CM) ],
[ '' , qw (M MM MMM) ]
];
my $ i = 0 ; my @res = ();
push @res , ( $ nums -> [ $ i ++ ] [ ( $ n % 10 , $ n = int ( $ n / 10 )) [ 0 ] ]) for 0 .. 3 ;
print reverse @res ;
import java.util. * ;
public class IntegerConverter {
public static String intToRoman ( int number ) {
if ( number > = 4000 || number <= 0 )
return null ;
StringBuilder result = new StringBuilder ();
for ( Integer key : units . descendingKeySet ()) {
while ( number > = key ) {
number - = key ;
result . append ( units . get ( key ));
}
}
return result . toString ();
}
private static final NavigableMap < Integer , String > units ;
static {
NavigableMap < Integer , String > initMap = new TreeMap <> ();
initMap . put ( 1000 , "M" );
initMap . put ( 900 , "CM" );
initMap . put ( 500 , "D" );
initMap . put ( 400 , "CD" );
initMap . put ( 100 , "C" );
initMap . put ( 90 , "XC" );
initMap . put ( 50 , "L" );
initMap . put ( 40 , "XL" );
initMap . put ( 10 , "X" );
initMap . put ( 9 , "IX" );
initMap . put ( 5 , "V" );
initMap . put ( 4 , "IV" );
initMap . put ( 1 , "I" );
units = Collections . unmodifiableNavigableMap ( initMap );
}
}
/// <summary>
/// The class is designed to convert Arabic numbers to Roman and vice versa
/// </summary>
/// <remarks>
/// <para> The class initially contains an alphabet of Roman numbers capable of defining Arabic numbers from 1 to 39999 </para>
/// <para> If you need to expand the range, then you can define additional notation for Roman numbers using
/// field <see cref = "Basic Roman Numbers" /> Basic Roman Numbers </remarks>
public static class Roman
{
/// <summary>
/// Alphabet of basic Roman numbers
/// <para> The alphabet is built as a dictionary. The dictionary key is an Arabic number (int), the value is the corresponding
/// roman number (string) </para>
/// </summary>
/// <remarks>
/// <para> Contains the Roman designation of the Arabic numbers 1 *, 4 *, 5 *, 9 * - where "*" represents 0 ... N zeros </para>
/// <para> When creating, it contains the notation of numbers from 1 to 10000 (I ... ↂ) Since in a Roman number one character cannot
/// occurring more than three times, you can initially convert the numbers from 1 to 39999 into the Roman format. </para>
/// <para> If you want to be able to work with a large number of Roman numbers, then you should add to the list
/// additional designations starting from 40,000 without missing elements 1 *, 4 *, 5 *, 9 *. </para>
/// </remarks>
public static SortedList < int , string > BaseRomanNumbers { get ; set ; }
static Roman Number ()
{
BaseRomanNumbers = new SortedList < int , string > ( 17 );
Basic Roman Numbers . Add ( 1 , "I" );
Basic Roman Numbers . Add ( 4 , "IV" );
Basic Roman Numbers . Add ( 5 , "V" );
Basic Roman Numbers . Add ( 9 , "IX" );
Basic Roman Numbers . Add ( 10 , "X" );
Basic Roman Numbers . Add ( 40 , "XL" );
Basic Roman Numbers . Add ( 50 , "L" );
Basic Roman Numbers . Add ( 90 , "XC" );
Basic Roman Numbers . Add ( 100 , "C" );
Basic Roman Numbers . Add ( 400 , "CD" );
Basic Roman Numbers . Add ( 500 , "D" );
Basic Roman Numbers . Add ( 900 , "CM" );
Basic Roman Numbers . Add ( 1000 , "M" );
Basic Roman Numbers . Add ( 4000 , "Mↁ" );
Basic Roman Numbers . Add ( 5000 , "ↁ" );
Basic Roman Numbers . Add ( 9000 , "Mↂ" );
Basic Roman Numbers . Add ( 10000 , "ↂ" );
}
/// <summary>
/// Calculates the maximum possible Roman number for the current alphabet of Roman numbers.
/// </summary>
/// <returns> The highest possible Roman number </returns>
public static uint MaximumRoman Number ()
{
int lastNumber = BasicRomanNumbers . Keys Last ();
int numberNullServer = int . Parse ( last Number . ToString (). Replace ( '0' , '\ 0' ));
int preliminary = 0 ;
switch ( numberNoZeros )
{
case 1 :
preliminary = last Number * 4 - 1 ;
break ;
case 4 :
case 9 :
preliminary = last Number ;
break ;
case 5 :
preliminary = lastNumber + lastNumber / 5 * 3 ;
break ;
default :
break ;
}
return uint . Parse ( tentative . ToString (). Replace ( '0' , '9' )) ;;
}
/// <summary>
/// Converts an integer to a Roman number
/// </summary>
/// <param name = "numberArab"> Arabic number to be converted to Roman notation </param>
/// <exception cref = "ArgumentOutOfRangeException"> Thrown when a number equal to "0" is passed as a parameter
/// or a number greater than the maximum Roman number. </exception>
/// <returns> A string representing a Roman number </returns>
public static string ArabicBRoman ( this int numberArab )
{
StringBuilder number Roman = new StringBuilder ();
// Exclude the "-" sign from the Arabic number and make it the first character of the Roman number
if ( numberArab < 0 )
{
Roman number . Append ( "-" );
numberArab = - number ofArab ;
}
if ( numberArab == 0 )
throw new ArgumentOutOfRangeException ( "numberArab" , numberArab ,
"Invalid argument value: Roman numbers cannot be equal \" 0 \ "" );
else if ( numberArab > MaximumRomanNumber ())
throw new ArgumentOutOfRangeException ( "numberArab" , numberArab ,
string Format ( "Invalid argument value: it is not possible to specify a Roman number greater than {0}" ,
MaximumRoman Number ()));
// We decompose the Arabic number into the Roman numbers that make it up and combine them into one line
var necessary Basic Roman Numbers =
from to in Basic Roman Numbers . Keys
where to <= number of Arab
orderby to descending
select to ;
foreach ( int tech in requiredBaseRoman Numbers )
{
while (( number of Arab / tech ) > = 1 )
{
numberArab - = tech ;
Roman number . Append ( Basic Roman Numbers [ tech ]);
}
}
return number Roman . ToString ();
}
/// <summary>
/// Converts Roman to Arabic
/// </summary>
/// <param name = "numberRoman"> Roman number to be converted to int type </param>
/// <exception cref = "FormatException"> Thrown when a non-Roman number is passed as a parameter </exception>
/// <returns> An integer representing the Arabic notation of a Roman number </returns>
public static int RomanArabic ( this string numberRoman )
{
int numberArab = 0 ;
sbyte negative = 1 ;
string rome = number Trim ();
if ( rome [ 0 ] == '-' )
{
negative = - 1 ;
Rome = Rome Substring ( 1 );
}
StringBuilder pattern of Roman Number = new StringBuilder ();
foreach ( int to in Basic Roman Numbers . Keys )
{
int index = Basic Roman Numbers . Keys IndexOf ( k );
string quantifier = "?" ;
if ( index == 0 || ( index % 4 ) == 0 )
quantifier = "{0,3}" ;
Roman number template . Insert ( 0 , string . Format ( "(? <{0}> ({1}) {2})?" , As ToString (),
BasicRomanNumbers [ k ], quantifier ));
}
// Ignore case + match should start at the beginning of the line
Roman number template . Insert ( 0 , "(? I) ^" );
// Match must be found at the end of the line
Roman number template . Append ( "$" );
// Simplified check. Does not check errors like IVII
if (! Regex . IsMatch ( Rome , Roman Number pattern . ToString ()))
throw new FormatException ( string . Format ( "Text \" {0} \ "is not a Roman number" , Roman number ));
Match number = Regex . Match ( Rome , Roman Number pattern . ToString ());
foreach ( int to in Basic Roman Numbers . Keys )
{
numberArab + = number . Groups [ to . ToString ()]. Length / Basic Roman Numbers [ to ]. Length * K ;
}
return numberArab * negative ;
}
}
Notes
- ↑ Numbers and number systems . Online Encyclopedia Krugosvet.
- ↑ M. Ya. Vygodsky “Handbook of Elementary Mathematics” Moscow 1958 State publishing house of physical and mathematical literature. p.62
- ↑ Unicode Standard, 15.3 (“For most purposes, it is preferable to compose the Roman numerals from sequences of the appropriate Latin letters.”)
- ↑ 1 2 Unicode Number Forms
- ↑ Perry, David J. Proposal to Add Additional Ancient Roman Characters to UCS .
- ↑ For the first two lines
- ↑ Chapter 31. Roman numeric notation :: Ideas for implementation
- ↑ "Science and Life" N12 1986 p. 95, V. Ptitsyn, Moscow
- ↑ Author - Kuznetsov Evgeny A.
- ↑ Author - Kuznetsov Evgeny A., 1992
See also
- Arabic numerals
- Symbols of ancient Roman monetary and weight units