Roman numerals/Encode: Difference between revisions
Content added Content deleted
m (→{{header|Tailspin}}: update to stricter typing) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 16: | Line 16: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V anums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] |
||
V rnums = ‘M CM D CD C XC L XL X IX V IV I’.split(‘ ’) |
V rnums = ‘M CM D CD C XC L XL X IX V IV I’.split(‘ ’) |
||
Line 30: | Line 30: | ||
1009, 1444, 1666, 1945, 1997, 1999, 2000, 2008, 2010, 2011, 2500, 3000, 3999] |
1009, 1444, 1666, 1945, 1997, 1999, 2000, 2008, 2010, 2011, 2500, 3000, 3999] |
||
L(val) test |
L(val) test |
||
print(val‘ - ’to_roman(val))</ |
print(val‘ - ’to_roman(val))</syntaxhighlight> |
||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
< |
<syntaxhighlight lang="8080asm"> org 100h |
||
jmp test |
jmp test |
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
Line 131: | Line 131: | ||
dgtbufdef: db 5,0 |
dgtbufdef: db 5,0 |
||
dgtbuf: ds 6 |
dgtbuf: ds 6 |
||
romanbuf:</ |
romanbuf:</syntaxhighlight> |
||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |
||
===Main and Supporting Functions=== |
===Main and Supporting Functions=== |
||
The main program and test values: 70,1776,2021,3999,4000 |
The main program and test values: 70,1776,2021,3999,4000 |
||
< |
<syntaxhighlight lang="asm"> mov ax,0070h |
||
call EncodeRoman |
call EncodeRoman |
||
mov si,offset StringRam |
mov si,offset StringRam |
||
Line 164: | Line 164: | ||
ReturnToDos ;macro that calls the int that exits dos</ |
ReturnToDos ;macro that calls the int that exits dos</syntaxhighlight> |
||
The <code>EncodeRoman</code> routine: |
The <code>EncodeRoman</code> routine: |
||
< |
<syntaxhighlight lang="asm">;ROMAN NUMERALS MODULE |
||
EncodeRoman: |
EncodeRoman: |
||
Line 340: | Line 340: | ||
ror al,cl ;AX = 0X0Yh |
ror al,cl ;AX = 0X0Yh |
||
pop cx |
pop cx |
||
ret</ |
ret</syntaxhighlight> |
||
Macros used: |
Macros used: |
||
< |
<syntaxhighlight lang="asm">pushall macro |
||
push ax |
push ax |
||
push bx |
push bx |
||
Line 362: | Line 362: | ||
pop bx |
pop bx |
||
pop ax |
pop ax |
||
endm</ |
endm</syntaxhighlight> |
||
===Output=== |
===Output=== |
||
{{out}} |
{{out}} |
||
Line 374: | Line 374: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">DEFINE PTR="CARD" |
||
CARD ARRAY arabic=[1000 900 500 400 100 90 50 40 10 9 5 4 1] |
CARD ARRAY arabic=[1000 900 500 400 100 90 50 40 10 9 5 4 1] |
||
PTR ARRAY roman(13) |
PTR ARRAY roman(13) |
||
Line 413: | Line 413: | ||
PrintF("%U=%S%E",data(i),r) |
PrintF("%U=%S%E",data(i),r) |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Roman_numerals_encode.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Roman_numerals_encode.png Screenshot from Atari 8-bit computer] |
||
Line 426: | Line 426: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">function arabic2roman(num:Number):String { |
||
var lookup:Object = {M:1000, CM:900, D:500, CD:400, C:100, XC:90, L:50, XL:40, X:10, IX:9, V:5, IV:4, I:1}; |
var lookup:Object = {M:1000, CM:900, D:500, CD:400, C:100, XC:90, L:50, XL:40, X:10, IX:9, V:5, IV:4, I:1}; |
||
var roman:String = "", i:String; |
var roman:String = "", i:String; |
||
Line 440: | Line 440: | ||
trace("2008 in roman is " + arabic2roman(2008)); |
trace("2008 in roman is " + arabic2roman(2008)); |
||
trace("1666 in roman is " + arabic2roman(1666)); |
trace("1666 in roman is " + arabic2roman(1666)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>1990 in roman is MCMXC |
<pre>1990 in roman is MCMXC |
||
Line 447: | Line 447: | ||
</pre> |
</pre> |
||
And the reverse: |
And the reverse: |
||
< |
<syntaxhighlight lang="actionscript">function roman2arabic(roman:String):Number { |
||
var romanArr:Array = roman.toUpperCase().split(''); |
var romanArr:Array = roman.toUpperCase().split(''); |
||
var lookup:Object = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000}; |
var lookup:Object = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000}; |
||
Line 459: | Line 459: | ||
trace("MCMXC in arabic is " + roman2arabic("MCMXC")); |
trace("MCMXC in arabic is " + roman2arabic("MCMXC")); |
||
trace("MMVIII in arabic is " + roman2arabic("MMVIII")); |
trace("MMVIII in arabic is " + roman2arabic("MMVIII")); |
||
trace("MDCLXVI in arabic is " + roman2arabic("MDCLXVI"));</ |
trace("MDCLXVI in arabic is " + roman2arabic("MDCLXVI"));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>MCMXC in arabic is 1990 |
<pre>MCMXC in arabic is 1990 |
||
Line 466: | Line 466: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Roman_Numeral_Test is |
procedure Roman_Numeral_Test is |
||
Line 498: | Line 498: | ||
Put_Line (To_Roman (25)); |
Put_Line (To_Roman (25)); |
||
Put_Line (To_Roman (944)); |
Put_Line (To_Roman (944)); |
||
end Roman_Numeral_Test;</ |
end Roman_Numeral_Test;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 512: | Line 512: | ||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}} |
||
< |
<syntaxhighlight lang="algol68">[]CHAR roman = "MDCLXVmdclxvi"; # UPPERCASE for thousands # |
||
[]CHAR adjust roman = "CCXXmmccxxii"; |
[]CHAR adjust roman = "CCXXmmccxxii"; |
||
[]INT arabic = (1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 50, 10, 5, 1); |
[]INT arabic = (1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 50, 10, 5, 1); |
||
Line 540: | Line 540: | ||
print((val, " - ", arabic to roman(val), new line)) |
print((val, " - ", arabic to roman(val), new line)) |
||
OD |
OD |
||
)</ |
)</syntaxhighlight> |
||
{{out}} (last example is manually wrapped): |
{{out}} (last example is manually wrapped): |
||
<pre style="height:30ex;overflow:scroll"> |
<pre style="height:30ex;overflow:scroll"> |
||
Line 641: | Line 641: | ||
{{works with|awtoc|any - tested with release [http://www.jampan.co.nz/~glyn/aw2c.tar.gz Mon Apr 27 14:25:27 NZST 2009]}} |
{{works with|awtoc|any - tested with release [http://www.jampan.co.nz/~glyn/aw2c.tar.gz Mon Apr 27 14:25:27 NZST 2009]}} |
||
<!-- This specimen was emailed to be by Glyn Webster > "Here's a Roman number procedure that would fit in:" --> |
<!-- This specimen was emailed to be by Glyn Webster > "Here's a Roman number procedure that would fit in:" --> |
||
< |
<syntaxhighlight lang="algolw">BEGIN |
||
PROCEDURE ROMAN (INTEGER VALUE NUMBER; STRING(15) RESULT CHARACTERS; INTEGER RESULT LENGTH); |
PROCEDURE ROMAN (INTEGER VALUE NUMBER; STRING(15) RESULT CHARACTERS; INTEGER RESULT LENGTH); |
||
Line 691: | Line 691: | ||
ROMAN(2009, S, I); WRITE(S, I); |
ROMAN(2009, S, I); WRITE(S, I); |
||
ROMAN(405, S, I); WRITE(S, I); |
ROMAN(405, S, I); WRITE(S, I); |
||
END.</ |
END.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 703: | Line 703: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">toRoman←{ |
||
⍝ Digits and corresponding values |
⍝ Digits and corresponding values |
||
ds←((⊢≠⊃)⊆⊢)' M CM D CD C XC L XL X IX V IV I' |
ds←((⊢≠⊃)⊆⊢)' M CM D CD C XC L XL X IX V IV I' |
||
Line 712: | Line 712: | ||
(d⊃ds),∇⍵-d⊃vs ⍝ While one exists, add it and subtract from number |
(d⊃ds),∇⍵-d⊃vs ⍝ While one exists, add it and subtract from number |
||
}⍵ |
}⍵ |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 724: | Line 724: | ||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
(mapAccumL version) |
(mapAccumL version) |
||
< |
<syntaxhighlight lang="applescript">------------------ ROMAN INTEGER STRINGS ----------------- |
||
-- roman :: Int -> String |
-- roman :: Int -> String |
||
Line 860: | Line 860: | ||
missing value |
missing value |
||
end if |
end if |
||
end snd</ |
end snd</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>{"MMXVI", "MCMXC", "MMVIII", "MM", "MDCLXVI"}</pre> |
<pre>{"MMXVI", "MCMXC", "MMVIII", "MM", "MDCLXVI"}</pre> |
||
Line 866: | Line 866: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="rebol">nums: [[1000 "M"] [900 "CM"] [500 "D"] [400 "CD"] [100 "C"] [90 "XC"] |
||
[50 "L"] [40 "XL"] [10 "X"] [9 "IX"] [5 "V"] [4 "IV"] [1 "I"]) |
[50 "L"] [40 "XL"] [10 "X"] [9 "IX"] [5 "V"] [4 "IV"] [1 "I"]) |
||
Line 892: | Line 892: | ||
1000 1009 1444 1666 1945 1997 1999 2000 2008 2010 2011 2500 |
1000 1009 1444 1666 1945 1997 1999 2000 2008 2010 2011 2500 |
||
3000 3999] 'n |
3000 3999] 'n |
||
-> print [n "->" toRoman n]</ |
-> print [n "->" toRoman n]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 953: | Line 953: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="autohotkey">MsgBox % stor(444) |
||
stor(value) |
stor(value) |
||
Line 980: | Line 980: | ||
} |
} |
||
Return result . "O" |
Return result . "O" |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Autolisp}}== |
=={{header|Autolisp}}== |
||
<syntaxhighlight lang="autolisp"> |
|||
<lang Autolisp> |
|||
(defun c:roman() (romanNumber (getint "\n Enter number > ")) |
(defun c:roman() (romanNumber (getint "\n Enter number > ")) |
||
(defun romanNumber (n / uni dec hun tho nstr strlist nlist rom) |
(defun romanNumber (n / uni dec hun tho nstr strlist nlist rom) |
||
Line 1,008: | Line 1,008: | ||
rom |
rom |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,018: | Line 1,018: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f ROMAN_NUMERALS_ENCODE.AWK |
# syntax: GAWK -f ROMAN_NUMERALS_ENCODE.AWK |
||
BEGIN { |
BEGIN { |
||
Line 1,045: | Line 1,045: | ||
return(roman1000[v] roman100[w] roman10[x] roman1[y]) |
return(roman1000[v] roman100[w] roman10[x] roman1[y]) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,055: | Line 1,055: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="gwbasic"> 1 N = 1990: GOSUB 5: PRINT N" = "V$ |
||
2 N = 2008: GOSUB 5: PRINT N" = "V$ |
2 N = 2008: GOSUB 5: PRINT N" = "V$ |
||
3 N = 1666: GOSUB 5: PRINT N" = "V$; |
3 N = 1666: GOSUB 5: PRINT N" = "V$; |
||
Line 1,061: | Line 1,061: | ||
5 V = N:V$ = "": FOR I = 0 TO 12: FOR L = 1 TO 0 STEP 0:A = VAL ( MID$ ("1E3900500400100+90+50+40+10+09+05+04+01",I * 3 + 1,3)) |
5 V = N:V$ = "": FOR I = 0 TO 12: FOR L = 1 TO 0 STEP 0:A = VAL ( MID$ ("1E3900500400100+90+50+40+10+09+05+04+01",I * 3 + 1,3)) |
||
6 L = (V - A) > = 0:V$ = V$ + MID$ ("M.CMD.CDC.XCL.XLX.IXV.IVI",I * 2 + 1,(I - INT (I / 2) * 2 + 1) * L):V = V - A * L: NEXT L,I |
6 L = (V - A) > = 0:V$ = V$ + MID$ ("M.CMD.CDC.XCL.XLX.IXV.IVI",I * 2 + 1,(I - INT (I / 2) * 2 + 1) * L):V = V - A * L: NEXT L,I |
||
7 RETURN</ |
7 RETURN</syntaxhighlight> |
||
==={{header|ASIC}}=== |
==={{header|ASIC}}=== |
||
{{trans|DWScript}} |
{{trans|DWScript}} |
||
< |
<syntaxhighlight lang="basic"> |
||
REM Roman numerals/Encode |
REM Roman numerals/Encode |
||
DIM Weights(12) |
DIM Weights(12) |
||
Line 1,107: | Line 1,107: | ||
ExitToRoman: |
ExitToRoman: |
||
RETURN |
RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
{{works with|Commodore BASIC|7.0}} |
{{works with|Commodore BASIC|7.0}} |
||
C-128 version: |
C-128 version: |
||
< |
<syntaxhighlight lang="basic">100 DIM RN$(12),NV(12) |
||
110 FOR I=0 TO 12 |
110 FOR I=0 TO 12 |
||
120 : READ RN$(I), NV(I) |
120 : READ RN$(I), NV(I) |
||
Line 1,137: | Line 1,137: | ||
330 : LOOP |
330 : LOOP |
||
340 : PRINT RN$;CHR$(13) |
340 : PRINT RN$;CHR$(13) |
||
350 LOOP</ |
350 LOOP</syntaxhighlight> |
||
{{works with|Commodore BASIC|3.5}} |
{{works with|Commodore BASIC|3.5}} |
||
C-16/116/Plus-4 version (BASIC 3.5 has DO/LOOP but not BEGIN/BEND) |
C-16/116/Plus-4 version (BASIC 3.5 has DO/LOOP but not BEGIN/BEND) |
||
< |
<syntaxhighlight lang="basic">100 DIM RN$(12),NV(12) |
||
110 FOR I=0 TO 12 |
110 FOR I=0 TO 12 |
||
120 : READ RN$(I), NV(I) |
120 : READ RN$(I), NV(I) |
||
Line 1,165: | Line 1,165: | ||
330 : LOOP |
330 : LOOP |
||
340 : PRINT RN$;CHR$(13) |
340 : PRINT RN$;CHR$(13) |
||
350 LOOP</ |
350 LOOP</syntaxhighlight> |
||
{{works with|Commodore BASIC|2.0}} |
{{works with|Commodore BASIC|2.0}} |
||
This version works on any Commodore, though the title banner should be adjusted to match the color and screen width of the particular machine. |
This version works on any Commodore, though the title banner should be adjusted to match the color and screen width of the particular machine. |
||
< |
<syntaxhighlight lang="basic">100 DIM RN$(12),NV(12) |
||
110 FOR I=0 TO 12 |
110 FOR I=0 TO 12 |
||
120 : READ RN$(I), NV(I) |
120 : READ RN$(I), NV(I) |
||
Line 1,194: | Line 1,194: | ||
340 : PRINT RN$;CHR$(13) |
340 : PRINT RN$;CHR$(13) |
||
350 GOTO 210 |
350 GOTO 210 |
||
</syntaxhighlight> |
|||
</lang> |
|||
The output is the same for all the above versions: |
The output is the same for all the above versions: |
||
Line 1,215: | Line 1,215: | ||
==={{header|FreeBASIC}}=== |
==={{header|FreeBASIC}}=== |
||
{{works with|FreeBASIC}} |
{{works with|FreeBASIC}} |
||
< |
<syntaxhighlight lang="freebasic"> |
||
DIM SHARED arabic(0 TO 12) AS Integer => {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 } |
DIM SHARED arabic(0 TO 12) AS Integer => {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 } |
||
DIM SHARED roman(0 TO 12) AS String*2 => {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"} |
DIM SHARED roman(0 TO 12) AS String*2 => {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"} |
||
Line 1,236: | Line 1,236: | ||
PRINT "1666 = "; toRoman(1666) |
PRINT "1666 = "; toRoman(1666) |
||
PRINT "3888 = "; toRoman(3888) |
PRINT "3888 = "; toRoman(3888) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,246: | Line 1,246: | ||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 PROGRAM "Roman.bas" |
||
110 DO |
110 DO |
||
120 PRINT :INPUT PROMPT "Enter an arabic number: ":N |
120 PRINT :INPUT PROMPT "Enter an arabic number: ":N |
||
Line 1,269: | Line 1,269: | ||
310 END DEF |
310 END DEF |
||
320 DATA 1000,"M",900,"CM",500,"D",400,"CD",100,"C",90,"XC" |
320 DATA 1000,"M",900,"CM",500,"D",400,"CD",100,"C",90,"XC" |
||
330 DATA 50,"L",40,"XL",10,"X",9,"IX",5,"V",4,"IV",1,"I"</ |
330 DATA 50,"L",40,"XL",10,"X",9,"IX",5,"V",4,"IV",1,"I"</syntaxhighlight> |
||
==={{header|Nascom BASIC}}=== |
==={{header|Nascom BASIC}}=== |
||
{{trans|DWScript}} |
{{trans|DWScript}} |
||
{{works with|Nascom ROM BASIC|4.7}} |
{{works with|Nascom ROM BASIC|4.7}} |
||
< |
<syntaxhighlight lang="basic"> |
||
10 REM Roman numerals/Encode |
10 REM Roman numerals/Encode |
||
20 DIM WEIGHTS(12),SYMBOLS$(12) |
20 DIM WEIGHTS(12),SYMBOLS$(12) |
||
Line 1,303: | Line 1,303: | ||
580 GOTO 520 |
580 GOTO 520 |
||
590 RETURN |
590 RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,312: | Line 1,312: | ||
==={{header|ZX Spectrum Basic}}=== |
==={{header|ZX Spectrum Basic}}=== |
||
< |
<syntaxhighlight lang="zxbasic"> 10 DATA 1000,"M",900,"CM" |
||
20 DATA 500,"D",400,"CD" |
20 DATA 500,"D",400,"CD" |
||
30 DATA 100,"C",90,"XC" |
30 DATA 100,"C",90,"XC" |
||
Line 1,328: | Line 1,328: | ||
150 GO TO 120 |
150 GO TO 120 |
||
160 NEXT I |
160 NEXT I |
||
170 PRINT VALUE;"=";V$</ |
170 PRINT VALUE;"=";V$</syntaxhighlight> |
||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
< |
<syntaxhighlight lang="bacon">OPTION BASE 1 |
||
GLOBAL roman$[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" } |
GLOBAL roman$[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" } |
||
Line 1,354: | Line 1,354: | ||
PRINT toroman$(2008) |
PRINT toroman$(2008) |
||
PRINT toroman$(1666) |
PRINT toroman$(1666) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,364: | Line 1,364: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{works with|BASIC256 }} |
{{works with|BASIC256 }} |
||
< |
<syntaxhighlight lang="basic256"> |
||
print 1666+" = "+convert$(1666) |
print 1666+" = "+convert$(1666) |
||
print 2008+" = "+convert$(2008) |
print 2008+" = "+convert$(2008) |
||
Line 1,381: | Line 1,381: | ||
next i |
next i |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,392: | Line 1,392: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
{{trans|BASIC}} |
{{trans|BASIC}} |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
Line 1,423: | Line 1,423: | ||
set result=!result!!rom%a%! |
set result=!result!!rom%a%! |
||
set /a value-=!arab%a%! |
set /a value-=!arab%a%! |
||
goto add_val</ |
goto add_val</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2009 = MMIX |
<pre>2009 = MMIX |
||
Line 1,430: | Line 1,430: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> PRINT ;1999, FNroman(1999) |
||
PRINT ;2012, FNroman(2012) |
PRINT ;2012, FNroman(2012) |
||
PRINT ;1666, FNroman(1666) |
PRINT ;1666, FNroman(1666) |
||
Line 1,447: | Line 1,447: | ||
ENDWHILE |
ENDWHILE |
||
NEXT |
NEXT |
||
= r$</ |
= r$</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,457: | Line 1,457: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let toroman(n, v) = valof |
let toroman(n, v) = valof |
||
Line 1,498: | Line 1,498: | ||
show(3888) |
show(3888) |
||
show(2021) |
show(2021) |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1666 = MDCLXVI |
<pre>1666 = MDCLXVI |
||
Line 1,510: | Line 1,510: | ||
Reads the number to convert from standard input. No range validation is performed. |
Reads the number to convert from standard input. No range validation is performed. |
||
< |
<syntaxhighlight lang="befunge">&>0\0>00p:#v_$ >:#,_ $ @ |
||
4-v >5+#:/#<\55+%:5/\5%: |
4-v >5+#:/#<\55+%:5/\5%: |
||
vv_$9+00g+5g\00g8+>5g\00 |
vv_$9+00g+5g\00g8+>5g\00 |
||
g>\20p>:10p00g \#v _20gv |
g>\20p>:10p00g \#v _20gv |
||
> 2+ v^-1g01\g5+8<^ +9 _ |
> 2+ v^-1g01\g5+8<^ +9 _ |
||
IVXLCDM</ |
IVXLCDM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,523: | Line 1,523: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
{{trans|APL}} |
{{trans|APL}} |
||
< |
<syntaxhighlight lang="bqn">⟨ToRoman⇐R⟩ ← { |
||
ds ← 1↓¨(¯1+`⊏⊸=)⊸⊔" I IV V IX X XL L XC C CD D CM M" |
ds ← 1↓¨(¯1+`⊏⊸=)⊸⊔" I IV V IX X XL L XC C CD D CM M" |
||
vs ← 1e3∾˜ ⥊1‿4‿5‿9×⌜˜10⋆↕3 |
vs ← 1e3∾˜ ⥊1‿4‿5‿9×⌜˜10⋆↕3 |
||
Line 1,530: | Line 1,530: | ||
(⊑⟜ds∾·𝕊𝕩-⊑⟜vs) 1-˜⊑vs⍋𝕩 |
(⊑⟜ds∾·𝕊𝕩-⊑⟜vs) 1-˜⊑vs⍋𝕩 |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out|Example use}} |
{{out|Example use}} |
||
<lang> ToRoman¨ 1990‿2008‿1666‿2021 |
<syntaxhighlight lang="text"> ToRoman¨ 1990‿2008‿1666‿2021 |
||
⟨ "MCMXC" "MMVIII" "MDCLXVI" "MMXXI" ⟩</ |
⟨ "MCMXC" "MMVIII" "MDCLXVI" "MMXXI" ⟩</syntaxhighlight> |
||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( ( encode |
||
= indian roman cifr tenfoldroman letter tenfold |
= indian roman cifr tenfoldroman letter tenfold |
||
. !arg:#?indian |
. !arg:#?indian |
||
Line 1,584: | Line 1,584: | ||
) |
) |
||
) |
) |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1990 MCMXC |
<pre>1990 MCMXC |
||
Line 1,596: | Line 1,596: | ||
===Naive solution=== |
===Naive solution=== |
||
This solution is a smart but does not return the number written as a string. |
This solution is a smart but does not return the number written as a string. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
Line 1,620: | Line 1,620: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Enter arabic number: |
<pre>Enter arabic number: |
||
Line 1,629: | Line 1,629: | ||
</pre> |
</pre> |
||
===Not thread-safe=== |
===Not thread-safe=== |
||
< |
<syntaxhighlight lang="c">#define _CRT_SECURE_NO_WARNINGS |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 1,717: | Line 1,717: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre>Write given numbers as Roman numerals. |
<pre>Write given numbers as Roman numerals. |
||
Line 1,740: | Line 1,740: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
class Program |
class Program |
||
{ |
{ |
||
Line 1,767: | Line 1,767: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
One-liner Mono REPL |
One-liner Mono REPL |
||
< |
<syntaxhighlight lang="csharp"> |
||
Func<int, string> toRoman = (number) => |
Func<int, string> toRoman = (number) => |
||
new Dictionary<int, string> |
new Dictionary<int, string> |
||
Line 1,788: | Line 1,788: | ||
{1, "I"} |
{1, "I"} |
||
}.Aggregate(new string('I', number), (m, _) => m.Replace(new string('I', _.Key), _.Value)); |
}.Aggregate(new string('I', number), (m, _) => m.Replace(new string('I', _.Key), _.Value)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,807: | Line 1,807: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
===C++ 98=== |
===C++ 98=== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
Line 1,847: | Line 1,847: | ||
std::cout << to_roman(i) << std::endl; |
std::cout << to_roman(i) << std::endl; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===C++ 11=== |
===C++ 11=== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
Line 1,881: | Line 1,881: | ||
for (int i = 0; i < 2018; i++) |
for (int i = 0; i < 2018; i++) |
||
std::cout << i << " --> " << to_roman(i) << std::endl; |
std::cout << i << " --> " << to_roman(i) << std::endl; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
< |
<syntaxhighlight lang="ceylon">shared void run() { |
||
class Numeral(shared Character char, shared Integer int) {} |
class Numeral(shared Character char, shared Integer int) {} |
||
Line 1,928: | Line 1,928: | ||
assert (toRoman(1990) == "MCMXC"); |
assert (toRoman(1990) == "MCMXC"); |
||
assert (toRoman(2008) == "MMVIII"); |
assert (toRoman(2008) == "MMVIII"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
The easiest way is to use the built-in cl-format function |
The easiest way is to use the built-in cl-format function |
||
< |
<syntaxhighlight lang="clojure">(def arabic->roman |
||
(partial clojure.pprint/cl-format nil "~@R")) |
(partial clojure.pprint/cl-format nil "~@R")) |
||
Line 1,938: | Line 1,938: | ||
;"CXXIII" |
;"CXXIII" |
||
(arabic->roman 99) |
(arabic->roman 99) |
||
;"XCIX"</ |
;"XCIX"</syntaxhighlight>Alternatively:<syntaxhighlight lang="clojure">(def roman-map |
||
(sorted-map |
(sorted-map |
||
1 "I", 4 "IV", 5 "V", 9 "IX", |
1 "I", 4 "IV", 5 "V", 9 "IX", |
||
Line 1,954: | Line 1,954: | ||
(int->roman 1999) |
(int->roman 1999) |
||
; "MCMXCIX"</ |
; "MCMXCIX"</syntaxhighlight> |
||
An alternate implementation: |
An alternate implementation: |
||
<syntaxhighlight lang="clojure"> |
|||
<lang Clojure> |
|||
(defn a2r [a] |
(defn a2r [a] |
||
(let [rv '(1000 500 100 50 10 5 1) |
(let [rv '(1000 500 100 50 10 5 1) |
||
Line 1,976: | Line 1,976: | ||
(and (< a v) (< a l)) (recur a (rest rv) (rest dv) r) |
(and (< a v) (< a l)) (recur a (rest rv) (rest dv) r) |
||
:else (recur (- a l) (rest rv) (rest dv) (str r (rm d) (rm v))))))))) |
:else (recur (- a l) (rest rv) (rest dv) (str r (rm d) (rm v))))))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Usage: |
Usage: |
||
<syntaxhighlight lang="clojure"> |
|||
<lang Clojure> |
|||
(a2r 1666) |
(a2r 1666) |
||
"MDCLXVI" |
"MDCLXVI" |
||
Line 1,986: | Line 1,986: | ||
(map a2r [1000 1 389 45]) |
(map a2r [1000 1 389 45]) |
||
("M" "I" "CCCLXXXIX" "XLV") |
("M" "I" "CCCLXXXIX" "XLV") |
||
</syntaxhighlight> |
|||
</lang> |
|||
An alternate implementation: |
An alternate implementation: |
||
<syntaxhighlight lang="clojure"> |
|||
<lang Clojure> |
|||
(def roman-map |
(def roman-map |
||
(sorted-map-by > |
(sorted-map-by > |
||
Line 2,011: | Line 2,011: | ||
(>= v e) (cons roman (a2r v n)) |
(>= v e) (cons roman (a2r v n)) |
||
(< v e) (cons roman (a2r v (rest n)))))))) |
(< v e) (cons roman (a2r v (rest n)))))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Usage: |
Usage: |
||
<syntaxhighlight lang="clojure"> |
|||
<lang Clojure> |
|||
(a2r 1666) |
(a2r 1666) |
||
"MDCLXVI" |
"MDCLXVI" |
||
Line 2,021: | Line 2,021: | ||
(map a2r [1000 1 389 45]) |
(map a2r [1000 1 389 45]) |
||
("M" "I" "CCCLXXXIX" "XLV") |
("M" "I" "CCCLXXXIX" "XLV") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">roman = cluster is encode |
||
rep = null |
rep = null |
||
Line 2,070: | Line 2,070: | ||
stream$putl(po, int$unparse(test) || " = " || roman$encode(test)) |
stream$putl(po, int$unparse(test) || " = " || roman$encode(test)) |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1666 = MDCLXVI |
<pre>1666 = MDCLXVI |
||
Line 2,081: | Line 2,081: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
<syntaxhighlight lang="cobol"> |
|||
<lang COBOL> |
|||
IDENTIFICATION DIVISION. |
IDENTIFICATION DIVISION. |
||
PROGRAM-ID. TOROMAN. |
PROGRAM-ID. TOROMAN. |
||
Line 2,135: | Line 2,135: | ||
end-perform |
end-perform |
||
. |
. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} (input was supplied via STDIN) |
{{out}} (input was supplied via STDIN) |
||
<pre> |
<pre> |
||
Line 2,155: | Line 2,155: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
decimal_to_roman = (n) -> |
decimal_to_roman = (n) -> |
||
# This should work for any positive integer, although it |
# This should work for any positive integer, although it |
||
Line 2,202: | Line 2,202: | ||
else |
else |
||
console.log "error for #{decimal}: #{roman} is wrong" |
console.log "error for #{decimal}: #{roman} is wrong" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun roman-numeral (n) |
||
(format nil "~@R" n))</ |
(format nil "~@R" n))</syntaxhighlight> |
||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
include "argv.coh"; |
include "argv.coh"; |
||
Line 2,267: | Line 2,267: | ||
print(decimalToRoman(number as uint16, &buffer as [uint8])); |
print(decimalToRoman(number as uint16, &buffer as [uint8])); |
||
print_nl(); |
print_nl(); |
||
end loop;</ |
end loop;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,277: | Line 2,277: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">string toRoman(int n) pure nothrow |
||
in { |
in { |
||
assert(n < 5000); |
assert(n < 5000); |
||
Line 2,302: | Line 2,302: | ||
} |
} |
||
void main() {}</ |
void main() {}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{trans|DWScript}} |
{{trans|DWScript}} |
||
< |
<syntaxhighlight lang="delphi">program RomanNumeralsEncode; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 2,333: | Line 2,333: | ||
Writeln(IntegerToRoman(2008)); // MMVIII |
Writeln(IntegerToRoman(2008)); // MMVIII |
||
Writeln(IntegerToRoman(1666)); // MDCLXVI |
Writeln(IntegerToRoman(1666)); // MDCLXVI |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="delphi">const weights = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; |
||
const symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]; |
const symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]; |
||
Line 2,356: | Line 2,356: | ||
PrintLn(toRoman(455)); |
PrintLn(toRoman(455)); |
||
PrintLn(toRoman(3456)); |
PrintLn(toRoman(3456)); |
||
PrintLn(toRoman(2488));</ |
PrintLn(toRoman(2488));</syntaxhighlight> |
||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<lang>values[] = [ 1000 900 500 400 100 90 50 40 10 9 5 4 1 ] |
<syntaxhighlight lang="text">values[] = [ 1000 900 500 400 100 90 50 40 10 9 5 4 1 ] |
||
symbol$[] = [ "M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I" ] |
symbol$[] = [ "M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I" ] |
||
func num2rom num . rom$ . |
func num2rom num . rom$ . |
||
Line 2,376: | Line 2,376: | ||
print r$ |
print r$ |
||
call num2rom 1666 r$ |
call num2rom 1666 r$ |
||
print r$</ |
print r$</syntaxhighlight> |
||
=={{header|ECL}}== |
=={{header|ECL}}== |
||
< |
<syntaxhighlight lang="ecl">RomanEncode(UNSIGNED Int) := FUNCTION |
||
SetWeights := [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; |
SetWeights := [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; |
||
SetSymbols := ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']; |
SetSymbols := ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']; |
||
Line 2,403: | Line 2,403: | ||
RomanEncode(1990 ); //MCMXC |
RomanEncode(1990 ); //MCMXC |
||
RomanEncode(2008 ); //MMVIII |
RomanEncode(2008 ); //MMVIII |
||
RomanEncode(1666); //MDCLXVI</ |
RomanEncode(1666); //MDCLXVI</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
< |
<syntaxhighlight lang="eiffel">class |
||
APPLICATION |
APPLICATION |
||
Line 2,464: | Line 2,464: | ||
Result := rnum |
Result := rnum |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Ela}}== |
=={{header|Ela}}== |
||
{{trans|Haskell}} |
{{trans|Haskell}} |
||
< |
<syntaxhighlight lang="ela">open number string math |
||
digit x y z k = |
digit x y z k = |
||
Line 2,483: | Line 2,483: | ||
| else = digit 'I' 'V' 'X' x |
| else = digit 'I' 'V' 'X' x |
||
map (join "" << toRoman) [1999,25,944]</ |
map (join "" << toRoman) [1999,25,944]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,491: | Line 2,491: | ||
{{trans|C#}} |
{{trans|C#}} |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import system'collections; |
||
import system'routines; |
import system'routines; |
||
import extensions; |
import extensions; |
||
Line 2,522: | Line 2,522: | ||
console.printLine("2008 : ", 2008.toRoman()); |
console.printLine("2008 : ", 2008.toRoman()); |
||
console.printLine("1666 : ", 1666.toRoman()) |
console.printLine("1666 : ", 1666.toRoman()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,532: | Line 2,532: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Erlang}} |
{{trans|Erlang}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Roman_numeral do |
||
def encode(0), do: '' |
def encode(0), do: '' |
||
def encode(x) when x >= 1000, do: [?M | encode(x - 1000)] |
def encode(x) when x >= 1000, do: [?M | encode(x - 1000)] |
||
Line 2,548: | Line 2,548: | ||
defp digit(8, x, y, _), do: [y, x, x, x] |
defp digit(8, x, y, _), do: [y, x, x, x] |
||
defp digit(9, x, _, z), do: [x, z] |
defp digit(9, x, _, z), do: [x, z] |
||
end</ |
end</syntaxhighlight> |
||
'''Another:''' |
'''Another:''' |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Roman_numeral do |
||
@symbols [ {1000, 'M'}, {900, 'CM'}, {500, 'D'}, {400, 'CD'}, {100, 'C'}, {90, 'XC'}, |
@symbols [ {1000, 'M'}, {900, 'CM'}, {500, 'D'}, {400, 'CD'}, {100, 'C'}, {90, 'XC'}, |
||
{50, 'L'}, {40, 'XL'}, {10, 'X'}, {9, 'IX'}, {5, 'V'}, {4, 'IV'}, {1, 'I'} ] |
{50, 'L'}, {40, 'XL'}, {10, 'X'}, {9, 'IX'}, {5, 'V'}, {4, 'IV'}, {1, 'I'} ] |
||
Line 2,561: | Line 2,561: | ||
Enum.join(roman) |
Enum.join(roman) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
'''Test:''' |
'''Test:''' |
||
< |
<syntaxhighlight lang="elixir">Enum.each([1990, 2008, 1666], fn n -> |
||
IO.puts "#{n}: #{Roman_numeral.encode(n)}" |
IO.puts "#{n}: #{Roman_numeral.encode(n)}" |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,576: | Line 2,576: | ||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun ar2ro (AN) |
||
"Translate from arabic number AN to roman number. |
"Translate from arabic number AN to roman number. |
||
For example, (ar2ro 1666) returns (M D C L X V I)." |
For example, (ar2ro 1666) returns (M D C L X V I)." |
||
Line 2,592: | Line 2,592: | ||
((>= AN 4) (cons 'I (cons 'V (ar2ro (- AN 4))))) |
((>= AN 4) (cons 'I (cons 'V (ar2ro (- AN 4))))) |
||
((>= AN 1) (cons 'I (ar2ro (- AN 1)))) |
((>= AN 1) (cons 'I (ar2ro (- AN 1)))) |
||
((= AN 0) nil)))</ |
((= AN 0) nil)))</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
{{trans|OCaml}} |
{{trans|OCaml}} |
||
< |
<syntaxhighlight lang="erlang">-module(roman). |
||
-export([to_roman/1]). |
-export([to_roman/1]). |
||
Line 2,615: | Line 2,615: | ||
digit(7, X, Y, _) -> [Y, X, X]; |
digit(7, X, Y, _) -> [Y, X, X]; |
||
digit(8, X, Y, _) -> [Y, X, X, X]; |
digit(8, X, Y, _) -> [Y, X, X, X]; |
||
digit(9, X, _, Z) -> [X, Z].</ |
digit(9, X, _, Z) -> [X, Z].</syntaxhighlight> |
||
sample: |
sample: |
||
Line 2,630: | Line 2,630: | ||
Alternative: |
Alternative: |
||
< |
<syntaxhighlight lang="erlang"> |
||
-module( roman_numerals ). |
-module( roman_numerals ). |
||
Line 2,651: | Line 2,651: | ||
map() -> [{"M",1000}, {"CM",900}, {"D",500}, {"CD",400}, {"C",100}, {"XC",90}, {"L",50}, {"XL",40}, {"X",10}, {"IX",9}, {"V",5}, {"IV",4}, {"I\ |
map() -> [{"M",1000}, {"CM",900}, {"D",500}, {"CD",400}, {"C",100}, {"XC",90}, {"L",50}, {"XL",40}, {"X",10}, {"IX",9}, {"V",5}, {"IV",4}, {"I\ |
||
",1}]. |
",1}]. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,664: | Line 2,664: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM ARAB2ROMAN |
PROGRAM ARAB2ROMAN |
||
Line 2,690: | Line 2,690: | ||
TOROMAN(3888->ANS$) PRINT("3888 = ";ANS$) |
TOROMAN(3888->ANS$) PRINT("3888 = ";ANS$) |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
{{trans|BASIC}} |
{{trans|BASIC}} |
||
< |
<syntaxhighlight lang="euphoria">constant arabic = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 } |
||
constant roman = {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"} |
constant roman = {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"} |
||
Line 2,711: | Line 2,711: | ||
printf(1,"%d = %s\n",{2009,toRoman(2009)}) |
printf(1,"%d = %s\n",{2009,toRoman(2009)}) |
||
printf(1,"%d = %s\n",{1666,toRoman(1666)}) |
printf(1,"%d = %s\n",{1666,toRoman(1666)}) |
||
printf(1,"%d = %s\n",{3888,toRoman(3888)})</ |
printf(1,"%d = %s\n",{3888,toRoman(3888)})</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,723: | Line 2,723: | ||
Excel can encode numbers in Roman forms in 5 successively concise forms. |
Excel can encode numbers in Roman forms in 5 successively concise forms. |
||
These can be indicated from 0 to 4. Type in a cell: |
These can be indicated from 0 to 4. Type in a cell: |
||
<syntaxhighlight lang="excel"> |
|||
<lang Excel> |
|||
=ROMAN(2013,0) |
=ROMAN(2013,0) |
||
</syntaxhighlight> |
|||
</lang> |
|||
It becomes: |
It becomes: |
||
<lang> |
<syntaxhighlight lang="text"> |
||
MMXIII |
MMXIII |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">let digit x y z = function |
||
1 -> x |
1 -> x |
||
| 2 -> x + x |
| 2 -> x + x |
||
Line 2,760: | Line 2,760: | ||
|> List.map (fun n -> roman n) |
|> List.map (fun n -> roman n) |
||
|> List.iter (printfn "%s") |
|> List.iter (printfn "%s") |
||
0</ |
0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>MCMXC |
<pre>MCMXC |
||
Line 2,768: | Line 2,768: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
A roman numeral library ships with Factor. |
A roman numeral library ships with Factor. |
||
< |
<syntaxhighlight lang="factor">USE: roman |
||
( scratchpad ) 3333 >roman . |
( scratchpad ) 3333 >roman . |
||
"mmmcccxxxiii"</ |
"mmmcccxxxiii"</syntaxhighlight> |
||
Parts of the implementation: |
Parts of the implementation: |
||
< |
<syntaxhighlight lang="factor">CONSTANT: roman-digits |
||
{ "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" } |
{ "m" "cm" "d" "cd" "c" "xc" "l" "xl" "x" "ix" "v" "iv" "i" } |
||
Line 2,789: | Line 2,789: | ||
roman-values roman-digits [ |
roman-values roman-digits [ |
||
[ /mod swap ] dip <repetition> concat |
[ /mod swap ] dip <repetition> concat |
||
] 2map "" concat-as nip ;</ |
] 2map "" concat-as nip ;</syntaxhighlight> |
||
=={{header|FALSE}}== |
=={{header|FALSE}}== |
||
< |
<syntaxhighlight lang="false">^$." " |
||
[$999>][1000- "M"]# |
[$999>][1000- "M"]# |
||
$899> [ 900-"CM"]? |
$899> [ 900-"CM"]? |
||
Line 2,805: | Line 2,805: | ||
$ 4> [ 5- "V"]? |
$ 4> [ 5- "V"]? |
||
$ 3> [ 4-"IV"]? |
$ 3> [ 4-"IV"]? |
||
[$ ][ 1- "I"]#%</ |
[$ ][ 1- "I"]#%</syntaxhighlight> |
||
=={{header|Fan}}== |
=={{header|Fan}}== |
||
<syntaxhighlight lang="fan">** |
|||
<lang Fan>** |
|||
** converts a number to its roman numeral representation |
** converts a number to its roman numeral representation |
||
** |
** |
||
Line 2,845: | Line 2,845: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: vector create ( n -- ) 0 do , loop does> ( n -- ) swap cells + @ execute ; |
||
\ these are ( numerals -- numerals ) |
\ these are ( numerals -- numerals ) |
||
: ,I dup c@ C, ; : ,V dup 1 + c@ C, ; : ,X dup 2 + c@ C, ; |
: ,I dup c@ C, ; : ,V dup 1 + c@ C, ; : ,X dup 2 + c@ C, ; |
||
Line 2,865: | Line 2,865: | ||
1999 roman type \ MCMXCIX |
1999 roman type \ MCMXCIX |
||
25 roman type \ XXV |
25 roman type \ XXV |
||
944 roman type \ CMXLIV</ |
944 roman type \ CMXLIV</syntaxhighlight> |
||
Alternative implementation |
Alternative implementation |
||
< |
<syntaxhighlight lang="forth">create romans 0 , 1 , 5 , 21 , 9 , 2 , 6 , 22 , 86 , 13 , |
||
does> swap cells + @ ; |
does> swap cells + @ ; |
||
Line 2,885: | Line 2,885: | ||
create (roman) 16 chars allot |
create (roman) 16 chars allot |
||
1999 (roman) >roman type cr</ |
1999 (roman) >roman type cr</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">program roman_numerals |
||
implicit none |
implicit none |
||
Line 2,923: | Line 2,923: | ||
end function roman |
end function roman |
||
end program roman_numerals</ |
end program roman_numerals</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,933: | Line 2,933: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Function romanEncode(n As Integer) As String |
Function romanEncode(n As Integer) As String |
||
Line 2,963: | Line 2,963: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,973: | Line 2,973: | ||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
< |
<syntaxhighlight lang="futurebasic">window 1 |
||
local fn DecimaltoRoman( decimal as short ) as Str15 |
local fn DecimaltoRoman( decimal as short ) as Str15 |
||
Line 3,008: | Line 3,008: | ||
print " 33 = "; fn DecimaltoRoman( 33 ) |
print " 33 = "; fn DecimaltoRoman( 33 ) |
||
HandleEvents</ |
HandleEvents</syntaxhighlight> |
||
Output: |
Output: |
||
Line 3,027: | Line 3,027: | ||
If you see boxes in the code below, those are supposed to be the Unicode combining overline (U+0305) and look like {{overline|IVXLCDM}}. Or, if you see overstruck combinations of letters, that's a different font rendering problem. (If you need roman numerals > 3999 reliably, it might best to stick to chiseling them in stone...) |
If you see boxes in the code below, those are supposed to be the Unicode combining overline (U+0305) and look like {{overline|IVXLCDM}}. Or, if you see overstruck combinations of letters, that's a different font rendering problem. (If you need roman numerals > 3999 reliably, it might best to stick to chiseling them in stone...) |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 3,065: | Line 3,065: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,074: | Line 3,074: | ||
=={{header|Golo}}== |
=={{header|Golo}}== |
||
< |
<syntaxhighlight lang="golo">#!/usr/bin/env golosh |
||
---- |
---- |
||
This module takes a decimal integer and converts it to a Roman numeral. |
This module takes a decimal integer and converts it to a Roman numeral. |
||
Line 3,132: | Line 3,132: | ||
println("2008 == MMVIII? " + (2008: encode() == "MMVIII")) |
println("2008 == MMVIII? " + (2008: encode() == "MMVIII")) |
||
println("1666 == MDCLXVI? " + (1666: encode() == "MDCLXVI")) |
println("1666 == MDCLXVI? " + (1666: encode() == "MDCLXVI")) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">symbols = [ 1:'I', 4:'IV', 5:'V', 9:'IX', 10:'X', 40:'XL', 50:'L', 90:'XC', 100:'C', 400:'CD', 500:'D', 900:'CM', 1000:'M' ] |
||
def roman(arabic) { |
def roman(arabic) { |
||
Line 3,162: | Line 3,162: | ||
assert roman(1666) == 'MDCLXVI' |
assert roman(1666) == 'MDCLXVI' |
||
assert roman(1990) == 'MCMXC' |
assert roman(1990) == 'MCMXC' |
||
assert roman(2008) == 'MMVIII'</ |
assert roman(2008) == 'MMVIII'</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 3,168: | Line 3,168: | ||
With an explicit decimal digit representation list: |
With an explicit decimal digit representation list: |
||
< |
<syntaxhighlight lang="haskell">digit :: Char -> Char -> Char -> Integer -> String |
||
digit x y z k = |
digit x y z k = |
||
[[x], [x, x], [x, x, x], [x, y], [y], [y, x], [y, x, x], [y, x, x, x], [x, z]] !! |
[[x], [x, x], [x, x, x], [x, y], [y], [y, x], [y, x, x], [y, x, x, x], [x, z]] !! |
||
Line 3,190: | Line 3,190: | ||
main :: IO () |
main :: IO () |
||
main = print $ toRoman <$> [1999, 25, 944]</ |
main = print $ toRoman <$> [1999, 25, 944]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>["MCMXCIX","XXV","CMXLIV"]</pre> |
<pre>["MCMXCIX","XXV","CMXLIV"]</pre> |
||
Line 3,196: | Line 3,196: | ||
or, defining '''romanFromInt''' in terms of mapAccumL |
or, defining '''romanFromInt''' in terms of mapAccumL |
||
< |
<syntaxhighlight lang="haskell">import Data.Bifunctor (first) |
||
import Data.List (mapAccumL) |
import Data.List (mapAccumL) |
||
import Data.Tuple (swap) |
import Data.Tuple (swap) |
||
Line 3,213: | Line 3,213: | ||
main :: IO () |
main :: IO () |
||
main = (putStrLn . unlines) (roman <$> [1666, 1990, 2008, 2016, 2018])</ |
main = (putStrLn . unlines) (roman <$> [1666, 1990, 2008, 2016, 2018])</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>MDCLXVI |
<pre>MDCLXVI |
||
Line 3,223: | Line 3,223: | ||
With the Roman patterns abstracted, and in a simple logic programming idiom: |
With the Roman patterns abstracted, and in a simple logic programming idiom: |
||
< |
<syntaxhighlight lang="haskell"> |
||
module Main where |
module Main where |
||
Line 3,283: | Line 3,283: | ||
(if roman == expected then "PASS" |
(if roman == expected then "PASS" |
||
else ("FAIL, expected " ++ (show expected))) ++ ")" |
else ("FAIL, expected " ++ (show expected))) ++ ")" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,292: | Line 3,292: | ||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">CHARACTER Roman*20 |
||
CALL RomanNumeral(1990, Roman) ! MCMXC |
CALL RomanNumeral(1990, Roman) ! MCMXC |
||
Line 3,313: | Line 3,313: | ||
ENDDO |
ENDDO |
||
ENDDO |
ENDDO |
||
END</ |
END</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">link numbers # commas, roman |
||
procedure main(arglist) |
procedure main(arglist) |
||
every x := !arglist do |
every x := !arglist do |
||
write(commas(x), " -> ",roman(x)|"*** can't convert to Roman numerals ***") |
write(commas(x), " -> ",roman(x)|"*** can't convert to Roman numerals ***") |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers.icn provides roman] as seen below and is based upon a James Gimple SNOBOL4 function. |
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers.icn provides roman] as seen below and is based upon a James Gimple SNOBOL4 function. |
||
< |
<syntaxhighlight lang="icon">procedure roman(n) #: convert integer to Roman numeral |
||
local arabic, result |
local arabic, result |
||
static equiv |
static equiv |
||
Line 3,337: | Line 3,337: | ||
result := map(result,"IVXLCDM","XLCDM**") || equiv[arabic + 1] |
result := map(result,"IVXLCDM","XLCDM**") || equiv[arabic + 1] |
||
if find("*",result) then fail else return result |
if find("*",result) then fail else return result |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,354: | Line 3,354: | ||
INTERCAL outputs numbers as Roman numerals by default, so this is surprisingly trivial for a language that generally tries to make things as difficult as possible. Although you do still have to <i>input</i> the numbers as spelled out digitwise in all caps. |
INTERCAL outputs numbers as Roman numerals by default, so this is surprisingly trivial for a language that generally tries to make things as difficult as possible. Although you do still have to <i>input</i> the numbers as spelled out digitwise in all caps. |
||
< |
<syntaxhighlight lang="intercal"> PLEASE WRITE IN .1 |
||
DO READ OUT .1 |
DO READ OUT .1 |
||
DO GIVE UP</ |
DO GIVE UP</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 3,369: | Line 3,369: | ||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="io">Roman := Object clone do ( |
||
nums := list(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) |
nums := list(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) |
||
rum := list("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I") |
rum := list("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I") |
||
Line 3,386: | Line 3,386: | ||
) |
) |
||
Roman numeral(1666) println</ |
Roman numeral(1666) println</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<tt>rfd</tt> obtains Roman numerals from decimals. |
<tt>rfd</tt> obtains Roman numerals from decimals. |
||
< |
<syntaxhighlight lang="j">R1000=. ;L:1 ,{ <@(<;._1);._2]0 :0 |
||
C CC CCC CD D DC DCC DCCC CM |
C CC CCC CD D DC DCC DCCC CM |
||
X XX XXX XL L LX LXX LXXX XC |
X XX XXX XL L LX LXX LXXX XC |
||
Line 3,397: | Line 3,397: | ||
) |
) |
||
rfd=: ('M' $~ <.@%&1000) , R1000 {::~ 1000&|</ |
rfd=: ('M' $~ <.@%&1000) , R1000 {::~ 1000&|</syntaxhighlight> |
||
Explanation: R1000's definition contains rows representing each of 10 different digits in the 100s, 10s and 1s column (the first entry in each row is blank -- each entry is preceded by a space). R1000 itself represents the first 1000 roman numerals (the cartesian product of these three rows of roman numeral "digits" which is constructed so that they are in numeric order. And the first entry -- zero -- is just blank). To convert a number to its roman numeral representation, we will separate the number into the integer part after dividing by 1000 (that's the number of 'M's we need) and the remainder after dividing by 1000 (which will be an index into R1000). |
Explanation: R1000's definition contains rows representing each of 10 different digits in the 100s, 10s and 1s column (the first entry in each row is blank -- each entry is preceded by a space). R1000 itself represents the first 1000 roman numerals (the cartesian product of these three rows of roman numeral "digits" which is constructed so that they are in numeric order. And the first entry -- zero -- is just blank). To convert a number to its roman numeral representation, we will separate the number into the integer part after dividing by 1000 (that's the number of 'M's we need) and the remainder after dividing by 1000 (which will be an index into R1000). |
||
For example:< |
For example:<syntaxhighlight lang="j"> rfd 1234 |
||
MCCXXXIV |
MCCXXXIV |
||
rfd 567 |
rfd 567 |
||
DLXVII |
DLXVII |
||
rfd 89 |
rfd 89 |
||
LXXXIX</ |
LXXXIX</syntaxhighlight> |
||
Derived from the [[j:Essays/Roman Numerals|J Wiki]]. Further examples of use will be found there. |
Derived from the [[j:Essays/Roman Numerals|J Wiki]]. Further examples of use will be found there. |
||
Line 3,415: | Line 3,415: | ||
The conversion function throws an IllegalArgumentException for non-positive numbers, since Java does not have unsigned primitives. |
The conversion function throws an IllegalArgumentException for non-positive numbers, since Java does not have unsigned primitives. |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
< |
<syntaxhighlight lang="java5">public class RN { |
||
enum Numeral { |
enum Numeral { |
||
Line 3,455: | Line 3,455: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1999 = MCMXCIX |
<pre>1999 = MCMXCIX |
||
Line 3,465: | Line 3,465: | ||
at RN.main(RN.java:38)</pre> |
at RN.main(RN.java:38)</pre> |
||
{{works with|Java|1.8+}} |
{{works with|Java|1.8+}} |
||
< |
<syntaxhighlight lang="java5">import java.util.Set; |
||
import java.util.EnumSet; |
import java.util.EnumSet; |
||
import java.util.Collections; |
import java.util.Collections; |
||
Line 3,523: | Line 3,523: | ||
LongStream.of(1999, 25, 944).forEach(RomanNumerals::test); |
LongStream.of(1999, 25, 944).forEach(RomanNumerals::test); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1999 = MCMXCIX |
<pre>1999 = MCMXCIX |
||
Line 3,538: | Line 3,538: | ||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
< |
<syntaxhighlight lang="javascript">var roman = { |
||
map: [ |
map: [ |
||
1000, 'M', 900, 'CM', 500, 'D', 400, 'CD', 100, 'C', 90, 'XC', |
1000, 'M', 900, 'CM', 500, 'D', 400, 'CD', 100, 'C', 90, 'XC', |
||
Line 3,555: | Line 3,555: | ||
} |
} |
||
roman.int_to_roman(1999); // "MCMXCIX"</ |
roman.int_to_roman(1999); // "MCMXCIX"</syntaxhighlight> |
||
====Functional composition==== |
====Functional composition==== |
||
< |
<syntaxhighlight lang="javascript">(function () { |
||
'use strict'; |
'use strict'; |
||
Line 3,607: | Line 3,607: | ||
romanTranscription); |
romanTranscription); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="javascript">["MMXVI", "MCMXC", "MMVIII", "XIV.IX.MMXV", "MM", "MDCLXVI"]</syntaxhighlight> |
||
===ES6=== |
===ES6=== |
||
Line 3,616: | Line 3,616: | ||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
(mapAccumL version) |
(mapAccumL version) |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 3,693: | Line 3,693: | ||
// MAIN -- |
// MAIN -- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>MDCLXVI |
<pre>MDCLXVI |
||
Line 3,703: | Line 3,703: | ||
====Declarative==== |
====Declarative==== |
||
< |
<syntaxhighlight lang="javascript">function toRoman(num) { |
||
return 'I' |
return 'I' |
||
.repeat(num) |
.repeat(num) |
||
Line 3,720: | Line 3,720: | ||
} |
} |
||
console.log(toRoman(1666));</ |
console.log(toRoman(1666));</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<lang |
<syntaxhighlight lang="javascript">MDCLXVI</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 3,738: | Line 3,738: | ||
===Easy-to-code version=== |
===Easy-to-code version=== |
||
< |
<syntaxhighlight lang="jq">def to_roman_numeral: |
||
def romans: |
def romans: |
||
[100000, "\u2188"], |
[100000, "\u2188"], |
||
Line 3,769: | Line 3,769: | ||
| .n = .n - $i ) ) |
| .n = .n - $i ) ) |
||
| .res |
| .res |
||
end ;</ |
end ;</syntaxhighlight> |
||
'''Test Cases''' |
'''Test Cases''' |
||
< |
<syntaxhighlight lang="jq">def testcases: [1668, 1990, 2008, 2020, 4444, 5000, 8999, 39999, 89999, 399999]; |
||
"Decimal => Roman:", |
"Decimal => Roman:", |
||
(testcases[] |
(testcases[] |
||
| " \(.) => \(to_roman_numeral)" )</ |
| " \(.) => \(to_roman_numeral)" )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,793: | Line 3,793: | ||
==="Orders of Magnitude" version=== |
==="Orders of Magnitude" version=== |
||
'''Translated from [[#Julia|Julia]]''' extended to 399,999 |
'''Translated from [[#Julia|Julia]]''' extended to 399,999 |
||
< |
<syntaxhighlight lang="jq">def digits: tostring | explode | map( [.]|implode|tonumber); |
||
# Non-negative integer to Roman numeral up to 399,999 |
# Non-negative integer to Roman numeral up to 399,999 |
||
def to_roman_numeral: |
def to_roman_numeral: |
||
Line 3,812: | Line 3,812: | ||
| .rnum |
| .rnum |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
This covers both Encode (toRoman) and Decode (fromRoman). |
This covers both Encode (toRoman) and Decode (fromRoman). |
||
< |
<syntaxhighlight lang="javascript">/* Roman numerals, in Jsish */ |
||
var Roman = { |
var Roman = { |
||
ord: ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'], |
ord: ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'], |
||
Line 3,872: | Line 3,872: | ||
Roman.toRoman(1666) ==> MDCLXVI |
Roman.toRoman(1666) ==> MDCLXVI |
||
=!EXPECTEND!= |
=!EXPECTEND!= |
||
*/</ |
*/</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,879: | Line 3,879: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Printf |
||
function romanencode(n::Integer) |
function romanencode(n::Integer) |
||
Line 3,912: | Line 3,912: | ||
for n in testcases |
for n in testcases |
||
@printf("%-4i => %s\n", n, romanencode(n)) |
@printf("%-4i => %s\n", n, romanencode(n)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,933: | Line 3,933: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">val romanNumerals = mapOf( |
||
1000 to "M", |
1000 to "M", |
||
900 to "CM", |
900 to "CM", |
||
Line 3,968: | Line 3,968: | ||
println(encode(1666)) |
println(encode(1666)) |
||
println(encode(2008)) |
println(encode(2008)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,977: | Line 3,977: | ||
</pre> |
</pre> |
||
Alternatively: |
Alternatively: |
||
< |
<syntaxhighlight lang="scala">fun Int.toRomanNumeral(): String { |
||
fun digit(k: Int, unit: String, five: String, ten: String): String { |
fun digit(k: Int, unit: String, five: String, ten: String): String { |
||
return when (k) { |
return when (k) { |
||
Line 3,995: | Line 3,995: | ||
else -> throw IllegalArgumentException("${this} not in range 0..3999") |
else -> throw IllegalArgumentException("${this} not in range 0..3999") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">define br => '\r' |
||
// encode roman |
// encode roman |
||
define encodeRoman(num::integer)::string => { |
define encodeRoman(num::integer)::string => { |
||
Line 4,016: | Line 4,016: | ||
'2008 in roman is '+encodeRoman(2008) |
'2008 in roman is '+encodeRoman(2008) |
||
br |
br |
||
'1666 in roman is '+encodeRoman(1666)</ |
'1666 in roman is '+encodeRoman(1666)</syntaxhighlight> |
||
=={{header|LaTeX}}== |
=={{header|LaTeX}}== |
||
The macro <code>\Roman</code> is defined for uppercase roman numeral, accepting as ''argument'' a name of an existing counter. |
The macro <code>\Roman</code> is defined for uppercase roman numeral, accepting as ''argument'' a name of an existing counter. |
||
< |
<syntaxhighlight lang="latex">\documentclass{minimal} |
||
\newcounter{currentyear} |
\newcounter{currentyear} |
||
\setcounter{currentyear}{\year} |
\setcounter{currentyear}{\year} |
||
\begin{document} |
\begin{document} |
||
Anno Domini \Roman{currentyear} |
Anno Domini \Roman{currentyear} |
||
\end{document}</ |
\end{document}</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
dim arabic( 12) |
dim arabic( 12) |
||
for i =0 to 12 |
for i =0 to 12 |
||
Line 4,061: | Line 4,061: | ||
toRoman$ =result$ |
toRoman$ =result$ |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
2009 MMIX |
2009 MMIX |
||
Line 4,069: | Line 4,069: | ||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
< |
<syntaxhighlight lang="livecode">function toRoman intNum |
||
local roman,numArabic |
local roman,numArabic |
||
put "M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I" into romans |
put "M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I" into romans |
||
Line 4,090: | Line 4,090: | ||
end repeat |
end repeat |
||
return cc |
return cc |
||
end repeatChar</ |
end repeatChar</syntaxhighlight> |
||
Examples |
Examples |
||
Line 4,099: | Line 4,099: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">make "roman.rules [ |
||
[1000 M] [900 CM] [500 D] [400 CD] |
[1000 M] [900 CM] [500 D] [400 CD] |
||
[ 100 C] [ 90 XC] [ 50 L] [ 40 XL] |
[ 100 C] [ 90 XC] [ 50 L] [ 40 XL] |
||
Line 4,110: | Line 4,110: | ||
if :n < first first :rules [output (roman :n bf :rules :acc)] |
if :n < first first :rules [output (roman :n bf :rules :acc)] |
||
output (roman :n - first first :rules :rules word :acc last first :rules) |
output (roman :n - first first :rules :rules word :acc last first :rules) |
||
end</ |
end</syntaxhighlight> |
||
{{works with|UCB Logo}} |
{{works with|UCB Logo}} |
||
< |
<syntaxhighlight lang="logo">make "patterns [[?] [? ?] [? ? ?] [? ?2] [?2] [?2 ?] [?2 ? ?] [?2 ? ? ?] [? ?3]] |
||
to digit :d :numerals |
to digit :d :numerals |
||
Line 4,130: | Line 4,130: | ||
print roman 1999 ; MCMXCIX |
print roman 1999 ; MCMXCIX |
||
print roman 25 ; XXV |
print roman 25 ; XXV |
||
print roman 944 ; CMXLIV</ |
print roman 944 ; CMXLIV</syntaxhighlight> |
||
=={{header|LOLCODE}}== |
=={{header|LOLCODE}}== |
||
< |
<syntaxhighlight lang="lolcode">HAI 1.2 |
||
I HAS A Romunz ITZ A BUKKIT |
I HAS A Romunz ITZ A BUKKIT |
||
Romunz HAS A SRS 0 ITZ "M" |
Romunz HAS A SRS 0 ITZ "M" |
||
Line 4,182: | Line 4,182: | ||
VISIBLE SMOOSH 1666 " = " I IZ Romunize YR 1666 MKAY MKAY |
VISIBLE SMOOSH 1666 " = " I IZ Romunize YR 1666 MKAY MKAY |
||
VISIBLE SMOOSH 3888 " = " I IZ Romunize YR 3888 MKAY MKAY |
VISIBLE SMOOSH 3888 " = " I IZ Romunize YR 3888 MKAY MKAY |
||
KTHXBYE</ |
KTHXBYE</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 4,190: | Line 4,190: | ||
=={{header|LotusScript}}== |
=={{header|LotusScript}}== |
||
< |
<syntaxhighlight lang="lss"> |
||
Function toRoman(value) As String |
Function toRoman(value) As String |
||
Dim arabic(12) As Integer |
Dim arabic(12) As Integer |
||
Line 4,235: | Line 4,235: | ||
End Function |
End Function |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">romans = { |
||
{1000, "M"}, |
{1000, "M"}, |
||
{900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, |
{900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, |
||
Line 4,253: | Line 4,253: | ||
end |
end |
||
end |
end |
||
print()</ |
print()</syntaxhighlight> |
||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">define(`roman',`ifelse(eval($1>=1000),1,`M`'roman(eval($1-1000))', |
||
`ifelse(eval($1>=900),1,`CM`'roman(eval($1-900))', |
`ifelse(eval($1>=900),1,`CM`'roman(eval($1-900))', |
||
`ifelse(eval($1>=500),1,`D`'roman(eval($1-500))', |
`ifelse(eval($1>=500),1,`D`'roman(eval($1-500))', |
||
Line 4,270: | Line 4,270: | ||
)')')')')')')')')')')')')dnl |
)')')')')')')')')')')')')dnl |
||
dnl |
dnl |
||
roman(3675)</ |
roman(3675)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,278: | Line 4,278: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">> for n in [ 1666, 1990, 2008 ] do printf( "%d\t%s\n", n, convert( n, 'roman' ) ) end: |
||
1666 MDCLXVI |
1666 MDCLXVI |
||
1990 MCMXC |
1990 MCMXC |
||
2008 MMVIII</ |
2008 MMVIII</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
RomanNumeral is a built-in function in the Wolfram language. Examples: |
RomanNumeral is a built-in function in the Wolfram language. Examples: |
||
< |
<syntaxhighlight lang="mathematica">RomanNumeral[4] |
||
RomanNumeral[99] |
RomanNumeral[99] |
||
RomanNumeral[1337] |
RomanNumeral[1337] |
||
RomanNumeral[1666] |
RomanNumeral[1666] |
||
RomanNumeral[6889]</ |
RomanNumeral[6889]</syntaxhighlight> |
||
gives back: |
gives back: |
||
<pre>IV |
<pre>IV |
||
Line 4,321: | Line 4,321: | ||
=== roman.m === |
=== roman.m === |
||
<syntaxhighlight lang="mercury"> |
|||
<lang Mercury> |
|||
:- module roman. |
:- module roman. |
||
Line 4,374: | Line 4,374: | ||
:- end_module roman. |
:- end_module roman. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 4,400: | Line 4,400: | ||
Another implementation using an algorithm inspired by [[#Erlang|the Erlang implementation]] could look like this: |
Another implementation using an algorithm inspired by [[#Erlang|the Erlang implementation]] could look like this: |
||
<syntaxhighlight lang="mercury"> |
|||
<lang Mercury> |
|||
:- module roman2. |
:- module roman2. |
||
Line 4,449: | Line 4,449: | ||
:- end_module roman2. |
:- end_module roman2. |
||
</syntaxhighlight> |
|||
</lang> |
|||
This implementation calculates the value of the thousands, then the hundreds, then the tens, then the ones. In each case it uses the <code>digit/4</code> function and some tricks with unification to build the appropriate list of characters for the digit and multiplier being targeted. |
This implementation calculates the value of the thousands, then the hundreds, then the tens, then the ones. In each case it uses the <code>digit/4</code> function and some tricks with unification to build the appropriate list of characters for the digit and multiplier being targeted. |
||
Line 4,457: | Line 4,457: | ||
=={{header|Microsoft Small Basic}}== |
=={{header|Microsoft Small Basic}}== |
||
{{trans|DWScript}} |
{{trans|DWScript}} |
||
< |
<syntaxhighlight lang="microsoftsmallbasic"> |
||
arabicNumeral = 1990 |
arabicNumeral = 1990 |
||
ConvertToRoman() |
ConvertToRoman() |
||
Line 4,505: | Line 4,505: | ||
EndWhile |
EndWhile |
||
EndSub |
EndSub |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,516: | Line 4,516: | ||
{{trans|DWScript}} |
{{trans|DWScript}} |
||
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}} |
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}} |
||
< |
<syntaxhighlight lang="modula2"> |
||
MODULE RomanNumeralsEncode; |
MODULE RomanNumeralsEncode; |
||
Line 4,563: | Line 4,563: | ||
ToRoman(3888, Numeral); WriteString(Numeral); WriteLn; (* MMMDCCCLXXXVIII *) |
ToRoman(3888, Numeral); WriteString(Numeral); WriteLn; (* MMMDCCCLXXXVIII *) |
||
END RomanNumeralsEncode. |
END RomanNumeralsEncode. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,572: | Line 4,572: | ||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang="mumps">TOROMAN(INPUT) |
||
;Converts INPUT into a Roman numeral. INPUT must be an integer between 1 and 3999 |
;Converts INPUT into a Roman numeral. INPUT must be an integer between 1 and 3999 |
||
;OUTPUT is the string to return |
;OUTPUT is the string to return |
||
Line 4,585: | Line 4,585: | ||
.FOR Q:CURRVAL<$PIECE(ROMANVAL,"^",I) SET OUTPUT=OUTPUT_$PIECE(ROMANNUM,"^",I),CURRVAL=CURRVAL-$PIECE(ROMANVAL,"^",I) |
.FOR Q:CURRVAL<$PIECE(ROMANVAL,"^",I) SET OUTPUT=OUTPUT_$PIECE(ROMANNUM,"^",I),CURRVAL=CURRVAL-$PIECE(ROMANVAL,"^",I) |
||
KILL I,CURRVAL |
KILL I,CURRVAL |
||
QUIT OUTPUT</ |
QUIT OUTPUT</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>USER>W $$ROMAN^ROSETTA(1666) |
<pre>USER>W $$ROMAN^ROSETTA(1666) |
||
Line 4,599: | Line 4,599: | ||
Another variant |
Another variant |
||
< |
<syntaxhighlight lang="mumps">TOROMAN(n) |
||
;return empty string if input parameter 'n' is not in 1-3999 |
;return empty string if input parameter 'n' is not in 1-3999 |
||
Quit:(n'?1.4N)!(n'<4000)!'n "" |
Quit:(n'?1.4N)!(n'<4000)!'n "" |
||
Line 4,609: | Line 4,609: | ||
. Set x=$Translate(x,"IVX",$Piece("IVX~XLC~CDM~M","~",p-j+1)) |
. Set x=$Translate(x,"IVX",$Piece("IVX~XLC~CDM~M","~",p-j+1)) |
||
. Set r=r_x |
. Set r=r_x |
||
Quit r</ |
Quit r</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
const nums = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"), |
const nums = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"), |
||
Line 4,630: | Line 4,630: | ||
1000, 1009, 1444, 1666, 1945, 1997, 1999, |
1000, 1009, 1444, 1666, 1945, 1997, 1999, |
||
2000, 2008, 2010, 2011, 2500, 3000, 3999]: |
2000, 2008, 2010, 2011, 2500, 3000, 3999]: |
||
echo ($i).align(4), ": ", i.toRoman</ |
echo ($i).align(4), ": ", i.toRoman</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,690: | Line 4,690: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|C sharp}} |
{{trans|C sharp}} |
||
< |
<syntaxhighlight lang="objeck"> |
||
bundle Default { |
bundle Default { |
||
class Roman { |
class Roman { |
||
Line 4,723: | Line 4,723: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 4,729: | Line 4,729: | ||
With an explicit decimal digit representation list: |
With an explicit decimal digit representation list: |
||
< |
<syntaxhighlight lang="ocaml">let digit x y z = function |
||
1 -> [x] |
1 -> [x] |
||
| 2 -> [x;x] |
| 2 -> [x;x] |
||
Line 4,751: | Line 4,751: | ||
digit 'X' 'L' 'C' (x / 10) @ to_roman (x mod 10) |
digit 'X' 'L' 'C' (x / 10) @ to_roman (x mod 10) |
||
else |
else |
||
digit 'I' 'V' 'X' x</ |
digit 'I' 'V' 'X' x</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,765: | Line 4,765: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">[ [1000,"M"], [900,"CM"], [500,"D"], [400,"CD"], [100,"C"], [90,"XC"], [50,"L"], [40,"XL"], [10,"X"], [9,"IX"], [5,"V"], [4,"IV"], [1,"I"] ] const: Romans |
||
: roman(n) |
: roman(n) |
||
| r | |
| r | |
||
StringBuffer new |
StringBuffer new |
||
Romans forEach: r [ while(r first n <=) [ r second << n r first - ->n ] ] ;</ |
Romans forEach: r [ while(r first n <=) [ r second << n r first - ->n ] ] ;</syntaxhighlight> |
||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
< |
<syntaxhighlight lang="progress">FUNCTION encodeRoman RETURNS CHAR ( |
||
i_i AS INT |
i_i AS INT |
||
): |
): |
||
Line 4,809: | Line 4,809: | ||
1666 encodeRoman( 1666 ) SKIP |
1666 encodeRoman( 1666 ) SKIP |
||
VIEW-AS ALERT-BOX. |
VIEW-AS ALERT-BOX. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>--------------------------- |
<pre>--------------------------- |
||
Line 4,824: | Line 4,824: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
{{trans|Haskell}} |
{{trans|Haskell}} |
||
< |
<syntaxhighlight lang="oz">declare |
||
fun {Digit X Y Z K} |
fun {Digit X Y Z K} |
||
unit([X] [X X] [X X X] [X Y] [Y] [Y X] [Y X X] [Y X X X] [X Z]) |
unit([X] [X X] [X X X] [X Y] [Y] [Y X] [Y X X] [Y X X X] [X Z]) |
||
Line 4,840: | Line 4,840: | ||
end |
end |
||
in |
in |
||
{ForAll {Map [1999 25 944] ToRoman} System.showInfo}</ |
{ForAll {Map [1999 25 944] ToRoman} System.showInfo}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Old-style Roman numerals |
Old-style Roman numerals |
||
< |
<syntaxhighlight lang="parigp">oldRoman(n)={ |
||
while(n>999999, |
while(n>999999, |
||
n-=1000000; |
n-=1000000; |
||
Line 4,898: | Line 4,898: | ||
); |
); |
||
print() |
print() |
||
};</ |
};</syntaxhighlight> |
||
This simple version of medieval Roman numerals does not handle large numbers. |
This simple version of medieval Roman numerals does not handle large numbers. |
||
< |
<syntaxhighlight lang="parigp">medievalRoman(n)={ |
||
while(n>999, |
while(n>999, |
||
n-=1000; |
n-=1000; |
||
Line 4,955: | Line 4,955: | ||
); |
); |
||
print() |
print() |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 4,962: | Line 4,962: | ||
=={{header|Peloton}}== |
=={{header|Peloton}}== |
||
Roman numbers are built in to Peloton as a particular form of national number. However, for the sake of the task the _RO opcode has been defined. |
Roman numbers are built in to Peloton as a particular form of national number. However, for the sake of the task the _RO opcode has been defined. |
||
< |
<syntaxhighlight lang="sgml"><@ DEFUDOLITLIT>_RO|__Transformer|<@ DEFKEYPAR>__NationalNumericID|2</@><@ LETRESCS%NNMPAR>...|1</@></@> |
||
<@ ENU$$DLSTLITLIT>1990,2008,1,2,64,124,1666,10001|,| |
<@ ENU$$DLSTLITLIT>1990,2008,1,2,64,124,1666,10001|,| |
||
<@ SAYELTLST>...</@> is <@ SAY_ROELTLSTLIT>...|RomanLowerUnicode</@> <@ SAY_ROELTLSTLIT>...|RomanUpperUnicode</@> <@ SAY_ROELTLSTLIT>...|RomanASCII</@> |
<@ SAYELTLST>...</@> is <@ SAY_ROELTLSTLIT>...|RomanLowerUnicode</@> <@ SAY_ROELTLSTLIT>...|RomanUpperUnicode</@> <@ SAY_ROELTLSTLIT>...|RomanASCII</@> |
||
</@></ |
</@></syntaxhighlight> |
||
Same code in padded-out, variable-length English dialect |
Same code in padded-out, variable-length English dialect |
||
< |
<syntaxhighlight lang="sgml"><# DEFINE USERDEFINEDOPCODE LITERAL LITERAL>_RO|__Transformer|<# DEFINE KEYWORD PARAMETER>__NationalNumericID|2</#><# LET RESULT CAST NATIONALNUMBER PARAMETER>...|1</#></#> |
||
<# ENUMERATION LAMBDASPECIFIEDDELMITER LIST LITERAL LITERAL>1990,2008,1,2,64,124,1666,10001|,| |
<# ENUMERATION LAMBDASPECIFIEDDELMITER LIST LITERAL LITERAL>1990,2008,1,2,64,124,1666,10001|,| |
||
<# SAY ELEMENT LIST>...</#> is <# SAY _RO ELEMENT LIST LITERAL>...|RomanLowerUnicode</#> <# SAY _RO ELEMENT LIST LITERAL>...|RomanUpperUnicode</#> <# SAY _RO ELEMENT LIST LITERAL>...|RomanASCII</#> |
<# SAY ELEMENT LIST>...</#> is <# SAY _RO ELEMENT LIST LITERAL>...|RomanLowerUnicode</#> <# SAY _RO ELEMENT LIST LITERAL>...|RomanUpperUnicode</#> <# SAY _RO ELEMENT LIST LITERAL>...|RomanASCII</#> |
||
</#></ |
</#></syntaxhighlight> |
||
{{out}} Notice here the three different ways of representing the results. |
{{out}} Notice here the three different ways of representing the results. |
||
Line 4,989: | Line 4,989: | ||
==== Simple program ==== |
==== Simple program ==== |
||
Simple, fast, produces same output as the Math::Roman module and the Raku example, less crazy than writing a Latin program, and doesn't require experimental modules like the Raku translation. |
Simple, fast, produces same output as the Math::Roman module and the Raku example, less crazy than writing a Latin program, and doesn't require experimental modules like the Raku translation. |
||
< |
<syntaxhighlight lang="perl">my @symbols = ( [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ); |
||
sub roman { |
sub roman { |
||
Line 5,002: | Line 5,002: | ||
} |
} |
||
say roman($_) for 1..2012;</ |
say roman($_) for 1..2012;</syntaxhighlight> |
||
==== Using a module ==== |
==== Using a module ==== |
||
< |
<syntaxhighlight lang="perl">use Math::Roman qw/roman/; |
||
say roman($_) for 1..2012'</ |
say roman($_) for 1..2012'</syntaxhighlight> |
||
==== Ported version of Raku ==== |
==== Ported version of Raku ==== |
||
< |
<syntaxhighlight lang="perl">use List::MoreUtils qw( natatime ); |
||
my %symbols = ( |
my %symbols = ( |
||
Line 5,031: | Line 5,031: | ||
}; |
}; |
||
print roman($_) . "\n" for 1..2012;</ |
print roman($_) . "\n" for 1..2012;</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">toRoman</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">toRoman</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">roman</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"M"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CM"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"D"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"C"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"L"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XL"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"X"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"IX"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"V"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"IV"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I"</span><span style="color: #0000FF;">},</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">roman</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"M"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"CM"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"D"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CD"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"C"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"L"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XL"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"X"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"IX"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"V"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"IV"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I"</span><span style="color: #0000FF;">},</span> |
||
Line 5,051: | Line 5,051: | ||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1990</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2008</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1666</span><span style="color: #0000FF;">},</span><span style="color: #000000;">toRoman</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1990</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2008</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1666</span><span style="color: #0000FF;">},</span><span style="color: #000000;">toRoman</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,058: | Line 5,058: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt |
||
def romanEnc /# n -- s #/ |
def romanEnc /# n -- s #/ |
||
Line 5,081: | Line 5,081: | ||
enddef |
enddef |
||
1968 romanEnc print</ |
1968 romanEnc print</syntaxhighlight> |
||
{{trans|Lua}} |
{{trans|Lua}} |
||
< |
<syntaxhighlight lang="phixmonti">def romanEnc /# n -- s #/ |
||
var k |
var k |
||
( ( 1000 "M" ) ( 900 "CM" ) ( 500 "D" ) ( 400 "CD" ) ( 100 "C" ) ( 90 "XC" ) |
( ( 1000 "M" ) ( 900 "CM" ) ( 500 "D" ) ( 400 "CD" ) ( 100 "C" ) ( 90 "XC" ) |
||
Line 5,100: | Line 5,100: | ||
enddef |
enddef |
||
1968 romanEnc</ |
1968 romanEnc</syntaxhighlight> |
||
Without vars |
Without vars |
||
< |
<syntaxhighlight lang="phixmonti">def romanEnc /# n -- s #/ |
||
>ps |
>ps |
||
( ( 1000 "M" ) ( 900 "CM" ) ( 500 "D" ) ( 400 "CD" ) ( 100 "C" ) ( 90 "XC" ) |
( ( 1000 "M" ) ( 900 "CM" ) ( 500 "D" ) ( 400 "CD" ) ( 100 "C" ) ( 90 "XC" ) |
||
Line 5,120: | Line 5,120: | ||
enddef |
enddef |
||
1968 romanEnc</ |
1968 romanEnc</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
{{works with|PHP|4+ tested in 5.2.12}} |
{{works with|PHP|4+ tested in 5.2.12}} |
||
< |
<syntaxhighlight lang="php"> |
||
/** |
/** |
||
* int2roman |
* int2roman |
||
Line 5,185: | Line 5,185: | ||
return $numeral . $leastSig; |
return $numeral . $leastSig; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">go => |
||
List = [455,999,1990,1999,2000,2001,2008,2009,2010,2011,2012,1666,3456,3888,4000], |
List = [455,999,1990,1999,2000,2001,2008,2009,2010,2011,2012,1666,3456,3888,4000], |
||
foreach(Val in List) |
foreach(Val in List) |
||
Line 5,208: | Line 5,208: | ||
end |
end |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,229: | Line 5,229: | ||
===Longest numeral=== |
===Longest numeral=== |
||
Which number encodes to the longest Roman numerals in the interval 1..4000: |
Which number encodes to the longest Roman numerals in the interval 1..4000: |
||
< |
<syntaxhighlight lang="picat">go2 => |
||
All = [Len=I=roman_encode(I) : I in 1..4000,E=roman_encode(I), Len=E.len].sort_down, |
All = [Len=I=roman_encode(I) : I in 1..4000,E=roman_encode(I), Len=E.len].sort_down, |
||
println(All[1..2]), |
println(All[1..2]), |
||
nl.</ |
nl.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,238: | Line 5,238: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de roman (N) |
||
(pack |
(pack |
||
(make |
(make |
||
Line 5,247: | Line 5,247: | ||
(link C) ) ) |
(link C) ) ) |
||
'(M CM D CD C XC L XL X IX V IV I) |
'(M CM D CD C XC L XL X IX V IV I) |
||
(1000 900 500 400 100 90 50 40 10 9 5 4 1) ) ) ) )</ |
(1000 900 500 400 100 90 50 40 10 9 5 4 1) ) ) ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>: (roman 1009) |
<pre>: (roman 1009) |
||
Line 5,256: | Line 5,256: | ||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">import String; |
||
int main(){ |
int main(){ |
||
write(int2roman(2009) + "\n"); |
write(int2roman(2009) + "\n"); |
||
write(int2roman(1666) + "\n"); |
write(int2roman(1666) + "\n"); |
||
write(int2roman(1337) + "\n"); |
write(int2roman(1337) + "\n"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
/* From Wiki Fortran */ |
/* From Wiki Fortran */ |
||
roman: procedure (n) returns(character (32) varying); |
roman: procedure (n) returns(character (32) varying); |
||
Line 5,284: | Line 5,284: | ||
return (r); |
return (r); |
||
end roman; |
end roman; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Results: |
Results: |
||
<pre> |
<pre> |
||
Line 5,295: | Line 5,295: | ||
=={{header|PL/SQL}}== |
=={{header|PL/SQL}}== |
||
<syntaxhighlight lang="pl/sql"> |
|||
<lang PL/SQL> |
|||
/***************************************************************** |
/***************************************************************** |
||
Line 5,321: | Line 5,321: | ||
END; |
END; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|plainTeX}}== |
=={{header|plainTeX}}== |
||
TeX has its own way to convert a number into roman numeral, but it produces lowercase letters; the following macro (and usage example), produce uppercase roman numeral. |
TeX has its own way to convert a number into roman numeral, but it produces lowercase letters; the following macro (and usage example), produce uppercase roman numeral. |
||
< |
<syntaxhighlight lang="tex">\def\upperroman#1{\uppercase\expandafter{\romannumeral#1}} |
||
Anno Domini \upperroman{\year} |
Anno Domini \upperroman{\year} |
||
\bye</ |
\bye</syntaxhighlight> |
||
=={{header|PowerBASIC}}== |
=={{header|PowerBASIC}}== |
||
Line 5,337: | Line 5,337: | ||
{{works with|PB/CC|5}} |
{{works with|PB/CC|5}} |
||
< |
<syntaxhighlight lang="powerbasic">FUNCTION toRoman(value AS INTEGER) AS STRING |
||
DIM arabic(0 TO 12) AS INTEGER |
DIM arabic(0 TO 12) AS INTEGER |
||
DIM roman(0 TO 12) AS STRING |
DIM roman(0 TO 12) AS STRING |
||
Line 5,360: | Line 5,360: | ||
? "1666 = " & toRoman(1666) |
? "1666 = " & toRoman(1666) |
||
? "3888 = " & toRoman(3888) |
? "3888 = " & toRoman(3888) |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
Filter ToRoman { |
Filter ToRoman { |
||
$output = '' |
$output = '' |
||
Line 5,401: | Line 5,401: | ||
$output |
$output |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
19,4,0,2479,3001 | ToRoman |
19,4,0,2479,3001 | ToRoman |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 5,418: | Line 5,418: | ||
{{libheader|clpfd}} |
{{libheader|clpfd}} |
||
Library clpfd assures that the program works in both managements : Roman towards Arabic and Arabic towards Roman. |
Library clpfd assures that the program works in both managements : Roman towards Arabic and Arabic towards Roman. |
||
< |
<syntaxhighlight lang="prolog">:- use_module(library(clpfd)). |
||
roman :- |
roman :- |
||
Line 5,520: | Line 5,520: | ||
my_print(A, R) :- |
my_print(A, R) :- |
||
format('~w in roman is ~w~n', [A, R]). |
format('~w in roman is ~w~n', [A, R]). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> ?- roman. |
<pre> ?- roman. |
||
Line 5,532: | Line 5,532: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">#SymbolCount = 12 ;0 based count |
||
DataSection |
DataSection |
||
denominations: |
denominations: |
||
Line 5,587: | Line 5,587: | ||
Input() |
Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Pythonic=== |
===Pythonic=== |
||
< |
<syntaxhighlight lang="python">import roman |
||
print(roman.toRoman(2022))</ |
print(roman.toRoman(2022))</syntaxhighlight> |
||
===Imperative=== |
===Imperative=== |
||
# Version for Python 2 |
# Version for Python 2 |
||
< |
<syntaxhighlight lang="python">roman = "MDCLXVmdclxvi"; # UPPERCASE for thousands # |
||
adjust_roman = "CCXXmmccxxii"; |
adjust_roman = "CCXXmmccxxii"; |
||
arabic = (1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 50, 10, 5, 1); |
arabic = (1000000, 500000, 100000, 50000, 10000, 5000, 1000, 500, 100, 50, 10, 5, 1); |
||
Line 5,619: | Line 5,619: | ||
2000,2008,2500,3000,4000,4999,5000,6666,10000,50000,100000,500000,1000000); |
2000,2008,2500,3000,4000,4999,5000,6666,10000,50000,100000,500000,1000000); |
||
for val in test: |
for val in test: |
||
print '%d - %s'%(val, arabic_to_roman(val))</ |
print '%d - %s'%(val, arabic_to_roman(val))</syntaxhighlight> |
||
An alternative which uses the divmod() function< |
An alternative which uses the divmod() function<syntaxhighlight lang="python">romanDgts= 'ivxlcdmVXLCDM_' |
||
def ToRoman(num): |
def ToRoman(num): |
||
Line 5,635: | Line 5,635: | ||
else: |
else: |
||
namoR += r*romanDgts[rdix] + (romanDgts[rdix+1] if(v==1) else '') |
namoR += r*romanDgts[rdix] + (romanDgts[rdix+1] if(v==1) else '') |
||
return namoR[-1::-1]</ |
return namoR[-1::-1]</syntaxhighlight> |
||
It is more Pythonic to use zip to iterate over two lists together: |
It is more Pythonic to use zip to iterate over two lists together: |
||
< |
<syntaxhighlight lang="python">anums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] |
||
rnums = "M CM D CD C XC L XL X IX V IV I".split() |
rnums = "M CM D CD C XC L XL X IX V IV I".split() |
||
Line 5,656: | Line 5,656: | ||
for val in test: |
for val in test: |
||
print '%d - %s'%(val, to_roman(val)) |
print '%d - %s'%(val, to_roman(val)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
# Version for Python 3 |
# Version for Python 3 |
||
< |
<syntaxhighlight lang="python">def arabic_to_roman(dclxvi): |
||
#=========================== |
#=========================== |
||
'''Convert an integer from the decimal notation to the Roman notation''' |
'''Convert an integer from the decimal notation to the Roman notation''' |
||
Line 5,680: | Line 5,680: | ||
for val in test: |
for val in test: |
||
print("%8d %s" %(val, arabic_to_roman(val)))</ |
print("%8d %s" %(val, arabic_to_roman(val)))</syntaxhighlight> |
||
===Declarative=== |
===Declarative=== |
||
Less readable, but a 'one liner': |
Less readable, but a 'one liner': |
||
< |
<syntaxhighlight lang="python">rnl = [ { '4' : 'MMMM', '3' : 'MMM', '2' : 'MM', '1' : 'M', '0' : '' }, { '9' : 'CM', '8' : 'DCCC', '7' : 'DCC', |
||
'6' : 'DC', '5' : 'D', '4' : 'CD', '3' : 'CCC', '2' : 'CC', '1' : 'C', '0' : '' }, { '9' : 'XC', |
'6' : 'DC', '5' : 'D', '4' : 'CD', '3' : 'CCC', '2' : 'CC', '1' : 'C', '0' : '' }, { '9' : 'XC', |
||
'8' : 'LXXX', '7' : 'LXX', '6' : 'LX', '5' : 'L', '4' : 'XL', '3' : 'XXX', '2' : 'XX', '1' : 'X', |
'8' : 'LXXX', '7' : 'LXX', '6' : 'LX', '5' : 'L', '4' : 'XL', '3' : 'XXX', '2' : 'XX', '1' : 'X', |
||
Line 5,694: | Line 5,694: | ||
# Option 2 |
# Option 2 |
||
def number2romannumeral(n): |
def number2romannumeral(n): |
||
return reduce(lambda x, y: x + y, map(lambda x, y: rnl[x][y], range(4), str(n).zfill(4))) if -1 < n < 5000 else None</ |
return reduce(lambda x, y: x + y, map(lambda x, y: rnl[x][y], range(4), str(n).zfill(4))) if -1 < n < 5000 else None</syntaxhighlight> |
||
Line 5,700: | Line 5,700: | ||
{{works with|Python|3}} |
{{works with|Python|3}} |
||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
< |
<syntaxhighlight lang="python">'''Encoding Roman Numerals''' |
||
from functools import reduce |
from functools import reduce |
||
Line 5,770: | Line 5,770: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>MDCLXVI |
<pre>MDCLXVI |
||
Line 5,780: | Line 5,780: | ||
=={{header|QBasic}}== |
=={{header|QBasic}}== |
||
< |
<syntaxhighlight lang="qbasic">DIM SHARED arabic(0 TO 12) |
||
DIM SHARED roman$(0 TO 12) |
DIM SHARED roman$(0 TO 12) |
||
Line 5,804: | Line 5,804: | ||
PRINT "2009 = "; toRoman$(2009) |
PRINT "2009 = "; toRoman$(2009) |
||
PRINT "1666 = "; toRoman$(1666) |
PRINT "1666 = "; toRoman$(1666) |
||
PRINT "3888 = "; toRoman$(3888)</ |
PRINT "3888 = "; toRoman$(3888)</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
Line 5,810: | Line 5,810: | ||
Pasting epitomised. |
Pasting epitomised. |
||
< |
<syntaxhighlight lang="quackery"> [ $ "" |
||
swap 1000 /mod $ "M" rot of rot swap join swap |
swap 1000 /mod $ "M" rot of rot swap join swap |
||
dup 900 < not if [ 900 - dip [ $ "CM" join ] ] |
dup 900 < not if [ 900 - dip [ $ "CM" join ] ] |
||
Line 5,828: | Line 5,828: | ||
1990 dup echo say " = " ->roman echo$ cr |
1990 dup echo say " = " ->roman echo$ cr |
||
2008 dup echo say " = " ->roman echo$ cr |
2008 dup echo say " = " ->roman echo$ cr |
||
1666 dup echo say " = " ->roman echo$ cr</ |
1666 dup echo say " = " ->roman echo$ cr</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 5,838: | Line 5,838: | ||
=={{header|R}}== |
=={{header|R}}== |
||
R has a built-in function, <code>[https://svn.r-project.org/R/trunk/src/library/utils/R/roman.R as.roman]</code>, for conversion to Roman numerals. The implementation details are found in <code>utils:::.numeric2roman</code> (see previous link), and <code>utils:::.roman2numeric</code>, for conversion back to Arabic decimals. |
R has a built-in function, <code>[https://svn.r-project.org/R/trunk/src/library/utils/R/roman.R as.roman]</code>, for conversion to Roman numerals. The implementation details are found in <code>utils:::.numeric2roman</code> (see previous link), and <code>utils:::.roman2numeric</code>, for conversion back to Arabic decimals. |
||
< |
<syntaxhighlight lang="r">as.roman(1666) # MDCLXVI</syntaxhighlight> |
||
Since the object <code>as.roman</code> creates is just an integer vector with a class, you can do arithmetic with Roman numerals: |
Since the object <code>as.roman</code> creates is just an integer vector with a class, you can do arithmetic with Roman numerals: |
||
< |
<syntaxhighlight lang="r">as.roman(1666) + 334 # MM</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Straight recursion: |
Straight recursion: |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (encode/roman number) |
(define (encode/roman number) |
||
(cond ((>= number 1000) (string-append "M" (encode/roman (- number 1000)))) |
(cond ((>= number 1000) (string-append "M" (encode/roman (- number 1000)))) |
||
Line 5,859: | Line 5,859: | ||
((>= number 4) (string-append "IV" (encode/roman (- number 4)))) |
((>= number 4) (string-append "IV" (encode/roman (- number 4)))) |
||
((>= number 1) (string-append "I" (encode/roman (- number 1)))) |
((>= number 1) (string-append "I" (encode/roman (- number 1)))) |
||
(else "")))</ |
(else "")))</syntaxhighlight> |
||
Using for/fold and quotient/remainder to remove repetition: |
Using for/fold and quotient/remainder to remove repetition: |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (number->list n) |
(define (number->list n) |
||
(for/fold ([result null]) |
(for/fold ([result null]) |
||
Line 5,879: | Line 5,879: | ||
1000 1009 1444 1666 1945 1997 1999 2000 2008 2010 2011 2500 |
1000 1009 1444 1666 1945 1997 1999 2000 2008 2010 2011 2500 |
||
3000 3999)]) |
3000 3999)]) |
||
(printf "~a ~a\n" n (encode/roman n)))</ |
(printf "~a ~a\n" n (encode/roman n)))</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>my %symbols = |
||
1 => "I", 5 => "V", 10 => "X", 50 => "L", 100 => "C", |
1 => "I", 5 => "V", 10 => "X", 50 => "L", 100 => "C", |
||
500 => "D", 1_000 => "M"; |
500 => "D", 1_000 => "M"; |
||
Line 5,905: | Line 5,905: | ||
for 1 .. 2_010 -> $x { |
for 1 .. 2_010 -> $x { |
||
say roman($x); |
say roman($x); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
Straight iterative solution: |
Straight iterative solution: |
||
<syntaxhighlight lang="red"> |
|||
<lang Red> |
|||
table: [1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 5 V 4 IV 1 I] |
table: [1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 5 V 4 IV 1 I] |
||
Line 5,919: | Line 5,919: | ||
foreach number [40 33 1888 2016][print [number ":" to-Roman number]] |
foreach number [40 33 1888 2016][print [number ":" to-Roman number]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
Straight recursive solution: |
Straight recursive solution: |
||
<syntaxhighlight lang="red"> |
|||
<lang Red> |
|||
table: [1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 5 V 4 IV 1 I] |
table: [1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 5 V 4 IV 1 I] |
||
Line 5,933: | Line 5,933: | ||
foreach number [40 33 1888 2016][print [number ":" to-Roman number]] |
foreach number [40 33 1888 2016][print [number ":" to-Roman number]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
This solution builds, using metaprogramming, a `case` table, that relies on recursion to convert every digit. |
This solution builds, using metaprogramming, a `case` table, that relies on recursion to convert every digit. |
||
<syntaxhighlight lang="red"> |
|||
<lang Red> |
|||
to-Roman: function [n [integer!]] reduce [ |
to-Roman: function [n [integer!]] reduce [ |
||
'case collect [ |
'case collect [ |
||
Line 5,946: | Line 5,946: | ||
foreach number [40 33 1888 2016][print [number ":" to-Roman number]] |
foreach number [40 33 1888 2016][print [number ":" to-Roman number]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Retro}}== |
=={{header|Retro}}== |
||
This is a port of the [[Forth]] code; but returns a string rather than displaying the roman numerals. It only handles numbers between 1 and 3999. |
This is a port of the [[Forth]] code; but returns a string rather than displaying the roman numerals. It only handles numbers between 1 and 3999. |
||
<syntaxhighlight lang="retro"> |
|||
<lang Retro> |
|||
: vector ( ...n"- ) |
: vector ( ...n"- ) |
||
here [ &, times ] dip : .data ` swap ` + ` @ ` do ` ; ; |
here [ &, times ] dip : .data ` swap ` + ` @ ` do ` ; ; |
||
Line 5,976: | Line 5,976: | ||
dup 1 3999 within 0 = |
dup 1 3999 within 0 = |
||
[ "EX LIMITO!\n" ] [ "IVXLCDM" swap record here ] if ; |
[ "EX LIMITO!\n" ] [ "IVXLCDM" swap record here ] if ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
< |
<syntaxhighlight lang="rexx">roman: procedure |
||
arg number |
arg number |
||
Line 5,996: | Line 5,996: | ||
end |
end |
||
end |
end |
||
return result</ |
return result</syntaxhighlight> |
||
===version 2=== |
===version 2=== |
||
This version of a REXX program allows almost any non-negative decimal integer. |
This version of a REXX program allows almost any non-negative decimal integer. |
||
Line 6,014: | Line 6,014: | ||
The general REXX code is bulkier than most at it deals with ''any'' non-negative decimal number, and more |
The general REXX code is bulkier than most at it deals with ''any'' non-negative decimal number, and more |
||
<br>boilerplate code is in the general REXX code to handle the above versions. |
<br>boilerplate code is in the general REXX code to handle the above versions. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program converts (Arabic) non─negative decimal integers (≥0) ───► Roman numerals.*/ |
||
numeric digits 10000 /*decimal digs can be higher if wanted.*/ |
numeric digits 10000 /*decimal digs can be higher if wanted.*/ |
||
parse arg # /*obtain optional integers from the CL.*/ |
parse arg # /*obtain optional integers from the CL.*/ |
||
Line 6,066: | Line 6,066: | ||
if pos(_, #)\==0 then #=changestr(_, #, copies('M', i)) |
if pos(_, #)\==0 then #=changestr(_, #, copies('M', i)) |
||
end /*i*/ |
end /*i*/ |
||
return #</ |
return #</syntaxhighlight> |
||
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. <br><br> |
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. <br><br> |
||
'''output''' when using the default (internal) input): |
'''output''' when using the default (internal) input): |
||
Line 6,164: | Line 6,164: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
arabic = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] |
arabic = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] |
||
roman = ["M", "CM", "D", "CD", "C" ,"XC", "L", "XL" ,"X", "IX", "V", "IV", "I"] |
roman = ["M", "CM", "D", "CD", "C" ,"XC", "L", "XL" ,"X", "IX", "V", "IV", "I"] |
||
Line 6,181: | Line 6,181: | ||
next |
next |
||
return result |
return result |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Roman numeral generation was used as an example for demonstrating [http://www.xpsd.org/cgi-bin/wiki?TestDrivenDevelopmentTutorialRomanNumerals Test Driven Development] in Ruby. The solution came to be: |
Roman numeral generation was used as an example for demonstrating [http://www.xpsd.org/cgi-bin/wiki?TestDrivenDevelopmentTutorialRomanNumerals Test Driven Development] in Ruby. The solution came to be: |
||
< |
<syntaxhighlight lang="ruby">Symbols = { 1=>'I', 5=>'V', 10=>'X', 50=>'L', 100=>'C', 500=>'D', 1000=>'M' } |
||
Subtractors = [ [1000, 100], [500, 100], [100, 10], [50, 10], [10, 1], [5, 1], [1, 0] ] |
Subtractors = [ [1000, 100], [500, 100], [100, 10], [50, 10], [10, 1], [5, 1], [1, 0] ] |
||
Line 6,198: | Line 6,198: | ||
[1990, 2008, 1666].each do |i| |
[1990, 2008, 1666].each do |i| |
||
puts "%4d => %s" % [i, roman(i)] |
puts "%4d => %s" % [i, roman(i)] |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,209: | Line 6,209: | ||
Another shorter version if we don't consider calculating the substractors: |
Another shorter version if we don't consider calculating the substractors: |
||
< |
<syntaxhighlight lang="ruby"> |
||
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ] |
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ] |
||
Line 6,216: | Line 6,216: | ||
Symbols.each { |arabic_rep, roman_rep| return roman_rep + arabic_to_roman(arabic - arabic_rep) if arabic >= arabic_rep } |
Symbols.each { |arabic_rep, roman_rep| return roman_rep + arabic_to_roman(arabic - arabic_rep) if arabic >= arabic_rep } |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
Yet another way to solve it in terms of reduce |
Yet another way to solve it in terms of reduce |
||
< |
<syntaxhighlight lang="ruby"> |
||
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ] |
Symbols = [ [1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I'] ] |
||
Line 6,229: | Line 6,229: | ||
end |
end |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">[loop] |
||
input "Input value:";val$ |
input "Input value:";val$ |
||
print roman$(val$) |
print roman$(val$) |
||
Line 6,251: | Line 6,251: | ||
wend |
wend |
||
next i |
next i |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">struct RomanNumeral { |
||
symbol: &'static str, |
symbol: &'static str, |
||
value: u32 |
value: u32 |
||
Line 6,292: | Line 6,292: | ||
println!("{:2$} = {}", n, to_roman(n), 4); |
println!("{:2$} = {}", n, to_roman(n), 4); |
||
} |
} |
||
}</ |
}</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
2014 = MMXIV |
2014 = MMXIV |
||
Line 6,303: | Line 6,303: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{works with|Scala|2.8}} |
{{works with|Scala|2.8}} |
||
< |
<syntaxhighlight lang="scala">val romanDigits = Map( |
||
1 -> "I", 5 -> "V", |
1 -> "I", 5 -> "V", |
||
10 -> "X", 50 -> "L", |
10 -> "X", 50 -> "L", |
||
Line 6,315: | Line 6,315: | ||
case Some(key) => romanDigits(key) + toRoman(n - key) |
case Some(key) => romanDigits(key) + toRoman(n - key) |
||
case None => "" |
case None => "" |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>scala> List(1990, 2008, 1666) map toRoman |
<pre>scala> List(1990, 2008, 1666) map toRoman |
||
res55: List[String] = List(MCMXC, MMVIII, MDCLXVI)</pre> |
res55: List[String] = List(MCMXC, MMVIII, MDCLXVI)</pre> |
||
===Using foldLeft=== |
===Using foldLeft=== |
||
< |
<syntaxhighlight lang="scala">def toRoman( v:Int ) : String = { |
||
val romanNumerals = List(1000->"M",900->"CM",500->"D",400->"CD",100->"C",90->"XC", |
val romanNumerals = List(1000->"M",900->"CM",500->"D",400->"CD",100->"C",90->"XC", |
||
50->"L",40->"XL",10->"X",9->"IX",5->"V",4->"IV",1->"I") |
50->"L",40->"XL",10->"X",9->"IX",5->"V",4->"IV",1->"I") |
||
Line 6,333: | Line 6,333: | ||
test(1990) |
test(1990) |
||
test(2008) |
test(2008) |
||
test(1666)</ |
test(1666)</syntaxhighlight> |
||
===Different code-style=== |
===Different code-style=== |
||
< |
<syntaxhighlight lang="scala">def toRoman(num: Int): String = { |
||
case class RomanUnit(value: Int, token: String) |
case class RomanUnit(value: Int, token: String) |
||
val romanNumerals = List( |
val romanNumerals = List( |
||
Line 6,360: | Line 6,360: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1990 => MCMXC |
<pre>1990 => MCMXC |
||
Line 6,369: | Line 6,369: | ||
This uses format directives supported in Chez Scheme since v6.9b; YMMV. |
This uses format directives supported in Chez Scheme since v6.9b; YMMV. |
||
< |
<syntaxhighlight lang="scheme">(define (to-roman n) |
||
(format "~@r" n))</ |
(format "~@r" n))</syntaxhighlight> |
||
This is a general example using Chicken Scheme. |
This is a general example using Chicken Scheme. |
||
< |
<syntaxhighlight lang="scheme">(define roman-decimal |
||
'(("M" . 1000) |
'(("M" . 1000) |
||
("CM" . 900) |
("CM" . 900) |
||
Line 6,407: | Line 6,407: | ||
(printf "~a ~a\n" (car n) (to-roman (car n))) |
(printf "~a ~a\n" (car n) (to-roman (car n))) |
||
(loop (cdr n)))) |
(loop (cdr n)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 6,415: | Line 6,415: | ||
which writes a roman numeral to a string. |
which writes a roman numeral to a string. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "stdio.s7i"; |
include "stdio.s7i"; |
||
include "wrinum.s7i"; |
include "wrinum.s7i"; |
||
Line 6,426: | Line 6,426: | ||
writeln(str(ROMAN, number)); |
writeln(str(ROMAN, number)); |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
Original source [http://seed7.sourceforge.net/algorith/puzzles.htm#roman_numerals]. |
Original source [http://seed7.sourceforge.net/algorith/puzzles.htm#roman_numerals]. |
||
Line 6,432: | Line 6,432: | ||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
< |
<syntaxhighlight lang="sensetalk">function RomanNumeralsEncode number |
||
put [ |
put [ |
||
(1, "I"), |
(1, "I"), |
||
Line 6,458: | Line 6,458: | ||
end repeat |
end repeat |
||
return numerals |
return numerals |
||
end RomanNumeralsEncode</ |
end RomanNumeralsEncode</syntaxhighlight> |
||
< |
<syntaxhighlight lang="sensetalk">repeat for each item in [ |
||
1990, |
1990, |
||
2008, |
2008, |
||
Line 6,466: | Line 6,466: | ||
] |
] |
||
put RomanNumeralsEncode(it) |
put RomanNumeralsEncode(it) |
||
end repeat</ |
end repeat</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,476: | Line 6,476: | ||
=={{header|SETL}}== |
=={{header|SETL}}== |
||
< |
<syntaxhighlight lang="ada">examples := [2008, 1666, 1990]; |
||
for example in examples loop |
for example in examples loop |
||
Line 6,492: | Line 6,492: | ||
end loop; |
end loop; |
||
return roman; |
return roman; |
||
end;</ |
end;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>MMVIII |
<pre>MMVIII |
||
Line 6,499: | Line 6,499: | ||
=={{header|Shen}}== |
=={{header|Shen}}== |
||
< |
<syntaxhighlight lang="shen"> |
||
(define encodeGlyphs |
(define encodeGlyphs |
||
ACC 0 _ -> ACC |
ACC 0 _ -> ACC |
||
Line 6,509: | Line 6,509: | ||
N -> (encodeGlyphs "" N ["M" 1000 "CM" 900 "D" 500 "CD" 400 "C" 100 "XC" 90 "L" 50 "XL" 40 "X" 10 "IX" 9 "V" 5 "IV" 4 "I" 1]) |
N -> (encodeGlyphs "" N ["M" 1000 "CM" 900 "D" 500 "CD" 400 "C" 100 "XC" 90 "L" 50 "XL" 40 "X" 10 "IX" 9 "V" 5 "IV" 4 "I" 1]) |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,524: | Line 6,524: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|ActionScript}} |
{{trans|ActionScript}} |
||
< |
<syntaxhighlight lang="ruby">func arabic2roman(num, roman='') { |
||
static lookup = [ |
static lookup = [ |
||
:M:1000, :CM:900, :D:500, |
:M:1000, :CM:900, :D:500, |
||
Line 6,542: | Line 6,542: | ||
say("1990 in roman is " + arabic2roman(1990)); |
say("1990 in roman is " + arabic2roman(1990)); |
||
say("2008 in roman is " + arabic2roman(2008)); |
say("2008 in roman is " + arabic2roman(2008)); |
||
say("1666 in roman is " + arabic2roman(1666));</ |
say("1666 in roman is " + arabic2roman(1666));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1990 in roman is MCMXC |
<pre>1990 in roman is MCMXC |
||
Line 6,549: | Line 6,549: | ||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
< |
<syntaxhighlight lang="simula">BEGIN |
||
TEXT PROCEDURE TOROMAN(N); INTEGER N; |
TEXT PROCEDURE TOROMAN(N); INTEGER N; |
||
Line 6,589: | Line 6,589: | ||
END PROGRAM; |
END PROGRAM; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,601: | Line 6,601: | ||
{{works with|Smalltalk/X}} |
{{works with|Smalltalk/X}} |
||
in ST/X, integers already know how to print themselves as roman number: |
in ST/X, integers already know how to print themselves as roman number: |
||
<lang |
<syntaxhighlight lang="smalltalk">2013 printRomanOn:Stdout naive:false</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
MMXIII</pre> |
MMXIII</pre> |
||
the implementation is: |
the implementation is: |
||
< |
<syntaxhighlight lang="smalltalk"> |
||
printRomanOn:aStream naive:naive |
printRomanOn:aStream naive:naive |
||
"print the receiver as roman number to the argument, aStream. |
"print the receiver as roman number to the argument, aStream. |
||
Line 6,660: | Line 6,660: | ||
] doWhile:[ repeatFlag and:[ restValue >= rValue] ]. |
] doWhile:[ repeatFlag and:[ restValue >= rValue] ]. |
||
]. |
]. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Adapted from [http://burks.bton.ac.uk/burks/language/snobol/catspaw/tutorial/ch6.htm Catspaw SNOBOL Tutorial, Chapter 6] |
Adapted from [http://burks.bton.ac.uk/burks/language/snobol/catspaw/tutorial/ch6.htm Catspaw SNOBOL Tutorial, Chapter 6] |
||
< |
<syntaxhighlight lang="snobol4"> |
||
* ROMAN(N) - Convert integer N to Roman numeral form. |
* ROMAN(N) - Convert integer N to Roman numeral form. |
||
* |
* |
||
Line 6,696: | Line 6,696: | ||
OUTPUT = " 944 = " ROMAN(944) |
OUTPUT = " 944 = " ROMAN(944) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,706: | Line 6,706: | ||
Here's a non-recursive version, and a Roman-to-Arabic converter to boot. |
Here's a non-recursive version, and a Roman-to-Arabic converter to boot. |
||
< |
<syntaxhighlight lang="snobol4">* # Arabic to Roman |
||
define('roman(n)s,ch,val,str') :(roman_end) |
define('roman(n)s,ch,val,str') :(roman_end) |
||
roman roman = ge(n,4000) n :s(return) |
roman roman = ge(n,4000) n :s(return) |
||
Line 6,734: | Line 6,734: | ||
astr = astr r '=' arabic(r) ' ' :(tloop) |
astr = astr r '=' arabic(r) ' ' :(tloop) |
||
out output = rstr; output = astr |
out output = rstr; output = astr |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,741: | Line 6,741: | ||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
< |
<syntaxhighlight lang="spl">a2r(a)= |
||
r = "" |
r = "" |
||
n = [["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"],[1000,900,500,400,100,90,50,40,10,9,5,4,1]] |
n = [["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"],[1000,900,500,400,100,90,50,40,10,9,5,4,1]] |
||
Line 6,756: | Line 6,756: | ||
> i, 1..#.size(t,1) |
> i, 1..#.size(t,1) |
||
#.output(t[i]," = ",a2r(t[i])) |
#.output(t[i]," = ",a2r(t[i])) |
||
<</ |
<</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,765: | Line 6,765: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
<syntaxhighlight lang="sql"> |
|||
<lang SQL> |
|||
-- |
-- |
||
-- This only works under Oracle and has the limitation of 1 to 3999 |
-- This only works under Oracle and has the limitation of 1 to 3999 |
||
Line 6,775: | Line 6,775: | ||
--------------- --------------- |
--------------- --------------- |
||
MDCLXVI mdclxvi |
MDCLXVI mdclxvi |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func ator(var n: Int) -> String { |
||
var result = "" |
var result = "" |
||
Line 6,803: | Line 6,803: | ||
} |
} |
||
return result |
return result |
||
}</ |
}</syntaxhighlight> |
||
Sample call: |
Sample call: |
||
{{works with|Swift|1.x}} |
{{works with|Swift|1.x}} |
||
< |
<syntaxhighlight lang="swift">println(ator(1666)) // MDCLXVI</syntaxhighlight> |
||
{{works with|Swift|2.0}} |
{{works with|Swift|2.0}} |
||
< |
<syntaxhighlight lang="swift">print(ator(1666)) // MDCLXVI</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre>MDCLXVI </pre> |
<pre>MDCLXVI </pre> |
||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
< |
<syntaxhighlight lang="tailspin"> |
||
def digits: [(M:1000"1"), (CM:900"1"), (D:500"1"), (CD:400"1"), (C:100"1"), (XC:90"1"), (L:50"1"), (XL:40"1"), (X:10"1"), (IX:9"1"), (V:5"1"), (IV:4"1"), (I:1"1")]; |
def digits: [(M:1000"1"), (CM:900"1"), (D:500"1"), (CD:400"1"), (C:100"1"), (XC:90"1"), (L:50"1"), (XL:40"1"), (X:10"1"), (IX:9"1"), (V:5"1"), (IV:4"1"), (I:1"1")]; |
||
templates encodeRoman |
templates encodeRoman |
||
Line 6,833: | Line 6,833: | ||
' -> !OUT::write |
' -> !OUT::write |
||
1666 -> encodeRoman -> !OUT::write |
1666 -> encodeRoman -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,842: | Line 6,842: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc to_roman {i} { |
||
set map {1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 9 IX 5 V 4 IV 1 I} |
set map {1000 M 900 CM 500 D 400 CD 100 C 90 XC 50 L 40 XL 10 X 9 IX 5 V 4 IV 1 I} |
||
foreach {value roman} $map { |
foreach {value roman} $map { |
||
Line 6,851: | Line 6,851: | ||
} |
} |
||
return $res |
return $res |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
< |
<syntaxhighlight lang="ti83b">PROGRAM:DEC2ROM |
||
:"="→Str1 |
:"="→Str1 |
||
:Lbl ST |
:Lbl ST |
||
Line 6,938: | Line 6,938: | ||
:Pause |
:Pause |
||
:Goto ST |
:Goto ST |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|True BASIC}}== |
=={{header|True BASIC}}== |
||
< |
<syntaxhighlight lang="qbasic">OPTION BASE 0 |
||
DIM arabic(12), roman$(12) |
DIM arabic(12), roman$(12) |
||
Line 6,966: | Line 6,966: | ||
PRINT "1666 = "; toRoman$(1666) |
PRINT "1666 = "; toRoman$(1666) |
||
PRINT "3888 = "; toRoman$(3888) |
PRINT "3888 = "; toRoman$(3888) |
||
END</ |
END</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
LOOP arab_number="1990'2008'1666" |
LOOP arab_number="1990'2008'1666" |
||
Line 6,976: | Line 6,976: | ||
PRINT "Arabic number ",arab_number, " equals ", roman_number |
PRINT "Arabic number ",arab_number, " equals ", roman_number |
||
ENDLOOP |
ENDLOOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,987: | Line 6,987: | ||
{{trans|DWScript}} |
{{trans|DWScript}} |
||
Weights and symbols in tuples. |
Weights and symbols in tuples. |
||
< |
<syntaxhighlight lang="javascript"> |
||
// Roman numerals/Encode |
// Roman numerals/Encode |
||
Line 7,011: | Line 7,011: | ||
console.log(toRoman(2022)); // MMXXII |
console.log(toRoman(2022)); // MMXXII |
||
console.log(toRoman(3888)); // MMMDCCCLXXXVIII |
console.log(toRoman(3888)); // MMMDCCCLXXXVIII |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 7,021: | Line 7,021: | ||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
{{trans|BBC Basic}} |
{{trans|BBC Basic}} |
||
<lang>Push 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 |
<syntaxhighlight lang="text">Push 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 |
||
' Initialize array |
' Initialize array |
||
For i = 12 To 0 Step -1 |
For i = 12 To 0 Step -1 |
||
Line 7,059: | Line 7,059: | ||
110 Print "D"; : Return |
110 Print "D"; : Return |
||
120 Print "CM"; : Return |
120 Print "CM"; : Return |
||
130 Print "M"; : Return</ |
130 Print "M"; : Return</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
{{works with|bash}} |
{{works with|bash}} |
||
< |
<syntaxhighlight lang="bash">roman() { |
||
local values=( 1000 900 500 400 100 90 50 40 10 9 5 4 1 ) |
local values=( 1000 900 500 400 100 90 50 40 10 9 5 4 1 ) |
||
local roman=( |
local roman=( |
||
Line 7,085: | Line 7,085: | ||
for test in 1999 24 944 1666 2008; do |
for test in 1999 24 944 1666 2008; do |
||
printf "%d = %s\n" $test $(roman $test) |
printf "%d = %s\n" $test $(roman $test) |
||
done</ |
done</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 7,103: | Line 7,103: | ||
CCCC are replaced by CD. The substitution operator (%=) is helpful |
CCCC are replaced by CD. The substitution operator (%=) is helpful |
||
here. |
here. |
||
< |
<syntaxhighlight lang="ursala">#import nat |
||
roman = |
roman = |
||
Line 7,109: | Line 7,109: | ||
-+ |
-+ |
||
'IIII'%='IV'+ 'VIIII'%='IX'+ 'XXXX'%='XL'+ 'LXXXX'%='XC'+ 'CCCC'%='CD'+ 'DCCCC'%='CM', |
'IIII'%='IV'+ 'VIIII'%='IX'+ 'XXXX'%='XL'+ 'LXXXX'%='XC'+ 'CCCC'%='CD'+ 'DCCCC'%='CM', |
||
~&plrDlSPSL/'MDCLXVI'+ iota*+ +^|(^|C/~&,\/division)@rlX=>~&iNC <1000,500,100,50,10,5>+-</ |
~&plrDlSPSL/'MDCLXVI'+ iota*+ +^|(^|C/~&,\/division)@rlX=>~&iNC <1000,500,100,50,10,5>+-</syntaxhighlight> |
||
This test program applies the function to each member of a list of numbers. |
This test program applies the function to each member of a list of numbers. |
||
< |
<syntaxhighlight lang="ursala">#show+ |
||
test = roman* <1990,2008,1,2,64,124,1666,10001></ |
test = roman* <1990,2008,1,2,64,124,1666,10001></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>MCMXC |
<pre>MCMXC |
||
Line 7,126: | Line 7,126: | ||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="vala">string to_roman(int n) |
||
requires (n > 0 && n < 5000) |
requires (n > 0 && n < 5000) |
||
{ |
{ |
||
Line 7,151: | Line 7,151: | ||
print("%s\n", to_roman(3456)); |
print("%s\n", to_roman(3456)); |
||
print("%s\n", to_roman(2488)); |
print("%s\n", to_roman(2488)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 7,161: | Line 7,161: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Private Function roman(n As Integer) As String |
||
roman = WorksheetFunction.roman(n) |
roman = WorksheetFunction.roman(n) |
||
End Function |
End Function |
||
Line 7,169: | Line 7,169: | ||
Debug.Print roman(CInt(x)); " "; |
Debug.Print roman(CInt(x)); " "; |
||
Next x |
Next x |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>X MMXVI DCCC MMDCCLXIX MDCLXVI CDLXXVI MCDLIII </pre> |
<pre>X MMXVI DCCC MMDCCLXIX MDCLXVI CDLXXVI MCDLIII </pre> |
||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
< |
<syntaxhighlight lang="vedit">// Main program for testing the function |
||
// |
// |
||
do { |
do { |
||
Line 7,204: | Line 7,204: | ||
} |
} |
||
Buf_Quit(OK) |
Buf_Quit(OK) |
||
Return</ |
Return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 7,216: | Line 7,216: | ||
{{trans|BASIC}} |
{{trans|BASIC}} |
||
< |
<syntaxhighlight lang="vb">Function toRoman(value) As String |
||
Dim arabic As Variant |
Dim arabic As Variant |
||
Dim roman As Variant |
Dim roman As Variant |
||
Line 7,237: | Line 7,237: | ||
Sub Main() |
Sub Main() |
||
MsgBox toRoman(Val(InputBox("Number, please"))) |
MsgBox toRoman(Val(InputBox("Number, please"))) |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="ecmascript">var romans = [ |
||
[1000, "M"], |
[1000, "M"], |
||
[900, "CM"], |
[900, "CM"], |
||
Line 7,272: | Line 7,272: | ||
System.print(encode.call(1666)) |
System.print(encode.call(1666)) |
||
System.print(encode.call(2008)) |
System.print(encode.call(2008)) |
||
System.print(encode.call(2020))</ |
System.print(encode.call(2020))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 7,285: | Line 7,285: | ||
{{trans|DWScript}} |
{{trans|DWScript}} |
||
{{works with|Windows XBasic}} |
{{works with|Windows XBasic}} |
||
< |
<syntaxhighlight lang="xbasic"> |
||
PROGRAM "romanenc" |
PROGRAM "romanenc" |
||
VERSION "0.0000" |
VERSION "0.0000" |
||
Line 7,344: | Line 7,344: | ||
END FUNCTION |
END FUNCTION |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 7,353: | Line 7,353: | ||
=={{header|XLISP}}== |
=={{header|XLISP}}== |
||
< |
<syntaxhighlight lang="lisp">(defun roman (n) |
||
(define roman-numerals '((1000 "m") (900 "cm") (500 "d") (400 "cd") (100 "c") (90 "xc") (50 "l") (40 "xl") (10 "x") (9 "ix") (5 "v") (4 "iv") (1 "i"))) |
(define roman-numerals '((1000 "m") (900 "cm") (500 "d") (400 "cd") (100 "c") (90 "xc") (50 "l") (40 "xl") (10 "x") (9 "ix") (5 "v") (4 "iv") (1 "i"))) |
||
(defun romanize (arabic-numeral numerals roman-numeral) |
(defun romanize (arabic-numeral numerals roman-numeral) |
||
Line 7,364: | Line 7,364: | ||
; test the function: |
; test the function: |
||
(display (mapcar roman '(10 2016 800 2769 1666 476 1453)))</ |
(display (mapcar roman '(10 2016 800 2769 1666 476 1453)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(x mmxvi dccc mmdcclxix mdclxvi cdlxxvi mcdliii)</pre> |
<pre>(x mmxvi dccc mmdcclxix mdclxvi cdlxxvi mcdliii)</pre> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">proc Rom(N, A, B, C); \Display 1..9 in Roman numerals |
||
int N, A, B, C, I; |
int N, A, B, C, I; |
||
[case N of |
[case N of |
||
Line 7,393: | Line 7,393: | ||
for I:= 0 to 7 do |
for I:= 0 to 7 do |
||
[IntOut(0, Tbl(I)); Text(0, ". "); Roman(Tbl(I)); CrLf(0)]; |
[IntOut(0, Tbl(I)); Text(0, ". "); Roman(Tbl(I)); CrLf(0)]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 7,408: | Line 7,408: | ||
=={{header|XSLT}}== |
=={{header|XSLT}}== |
||
< |
<syntaxhighlight lang="xslt"> |
||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
||
<xsl:template match="/data/number"> |
<xsl:template match="/data/number"> |
||
Line 7,459: | Line 7,459: | ||
</xsl:template> |
</xsl:template> |
||
</xsl:stylesheet> |
</xsl:stylesheet> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">roman$ = "M, CM, D, CD, C, XC, L, XL, X, IX, V, IV, I" |
||
decml$ = "1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1" |
decml$ = "1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1" |
||
Line 7,492: | Line 7,492: | ||
// 2009 = MMIX |
// 2009 = MMIX |
||
// 1666 = MDCLXVI |
// 1666 = MDCLXVI |
||
// 3888 = MMMDCCCLXXXVIII</ |
// 3888 = MMMDCCCLXXXVIII</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">var [const] romans = L( |
||
L("M", 1000), L("CM", 900), L("D", 500), L("CD", 400), L("C", 100), |
L("M", 1000), L("CM", 900), L("D", 500), L("CD", 400), L("C", 100), |
||
L("XC", 90), L("L", 50), L("XL", 40), L("X", 10), L("IX", 9), |
L("XC", 90), L("L", 50), L("XL", 40), L("X", 10), L("IX", 9), |
||
Line 7,503: | Line 7,503: | ||
foreach R,N in (romans){ text += R*(i/N); i = i%N; } |
foreach R,N in (romans){ text += R*(i/N); i = i%N; } |
||
return(text); |
return(text); |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
toRoman(1990) //-->"MCMXC" |
toRoman(1990) //-->"MCMXC" |
||
Line 7,511: | Line 7,511: | ||
=={{header|Zoea}}== |
=={{header|Zoea}}== |
||
<syntaxhighlight lang="zoea"> |
|||
<lang Zoea> |
|||
program: decimal_roman |
program: decimal_roman |
||
input: 12 |
input: 12 |
||
output: 'XII' |
output: 'XII' |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Zoea Visual}}== |
=={{header|Zoea Visual}}== |
||
Line 7,522: | Line 7,522: | ||
=={{header|Zsh}}== |
=={{header|Zsh}}== |
||
Based on the python solution. |
Based on the python solution. |
||
< |
<syntaxhighlight lang="zsh">function printroman () { |
||
local -a conv |
local -a conv |
||
local number=$1 div rom num out |
local number=$1 div rom num out |
||
Line 7,533: | Line 7,533: | ||
done |
done |
||
echo $out |
echo $out |
||
}</ |
}</syntaxhighlight> |