Roman numerals/Encode: Difference between revisions
m
→{{header|Phix}}: use pygments, added cheat version
PatGarrett (talk | contribs) |
m (→{{header|Phix}}: use pygments, added cheat version) |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 3,130:
<syntaxhighlight lang="text">
func$
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" ]
for i = 1 to len values[]
return rom$
.
print
print dec2rom 1666
</syntaxhighlight>
Line 3,261 ⟶ 3,258:
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
Line 3,284 ⟶ 3,281:
extension op
{
}
Line 5,701 ⟶ 5,698:
=={{header|Phix}}==
<!--
with javascript_semantics
function toRoman(integer v)
sequence 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 }
string res = ""
integer val = v
for i=1 to length(roman) do
while val>=decml[i] do
res &= roman[i]
val -= decml[i]
end while
end for
return {v,res} -- (for output)
end function
?apply({1990,2008,1666},toRoman)
▲<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
<pre>
{{1990,"MCMXC"},{2008,"MMVIII"},{1666,"MDCLXVI"}}
</pre>
=== cheating slightly ===
<syntaxhighlight lang="phix">
with javascript_semantics
requires("1.0.5")
function toRoman(integer n)
return {n,sprintf("%R",n)}
end function
</syntaxhighlight>
same output (builtins\VM\pprntfN.e/toRoman() is somewhat more obfuscated and faster than the above)
=={{header|Phixmonti}}==
Line 7,466 ⟶ 7,473:
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
@: 1
'$ -> ($)"1" -> #;' !
when <$digits($@)::value..> do
Line 7,472 ⟶ 7,479:
$ - $digits($@)::value -> #
when <1"1"..> do
@:$@ + 1
$ -> #
end encodeRoman
Line 7,704 ⟶ 7,711:
1990 = MCMXC
2011 = MMXI</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
const 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"}
fn main() {
println(encode(1990))
println(encode(2008))
println(encode(1666))
}
fn encode(number int) string {
mut num := number
mut result := ""
if number < 1 || number > 5000 {return result}
for digit, roman in numerals {
for num >= digit {
num -= digit
result += roman
}
}
return result
}
</syntaxhighlight>
{{out}}
<pre>
MCMXC
MMVIII
MDCLXVI
</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="
[1000, "M"],
[900, "CM"],
Line 7,856 ⟶ 7,895:
</xsl:stylesheet>
</syntaxhighlight>
=={{header|zkl}}==
|