Dengan bilangan bulat yang benar-benar positif, kembalikan angka Romawi sesingkat mungkin hanya dengan menggunakan aturan tambahan. Output harus terdiri dari nol atau lebih dari masing-masing karakter MDCLXVI
dalam urutan itu. 14
Karenanya, angka itu harus memberi XIIII
daripada XIV
.
Nilai numerik karakter adalah M
= 1000, D
= 500, C
= 100, L
= 50, X
= 10, V
= 5, I
= 1.
Contohnya
3
→ III
4
→ IIII
9
→ VIIII
42
→ XXXXII
796
→ DCCLXXXXVI
2017
→ MMXVII
16807
→ MMMMMMMMMMMMMMMMDCCCVII
4 -> IIII
adalah9 -> VIIII
juga bukanIX
?VIIII
adalah satu-satunya output yang diizinkan untuk 9.Jawaban:
Plain English ,
10591025678641451399 byteDisimpan 34 byte dengan menghapus perangkap kesalahan. Kemudian disimpan 384 byte dengan bermain golf. Kemudian disimpan 190 byte dengan menggabungkan operasi divisi dengan operasi append ("z") ke dalam operasi baru ("p"). Kemudian disimpan 52 byte dengan bermain golf.
Ini adalah versi terakhir dari kode final, ditambah jebakan kesalahan untuk angka negatif:
sumber
APL (Dyalog),
2522 bytesTry it online!
sumber
/
) instead of reshape (⍴
) so you can cut out the each and the catenate-reduction (¨
and,/
).⎕
) and use commute (⍨
) to remove the parens and compose (∘
).{}
or∇f∇
surrounding the functionRetina,
5742 bytesConverts to unary, then greedily replaces bunches of
I
s with the higher denominations in order.Try it online
Saved 15 bytes thanks to Martin
sumber
I
as unit?Python 2, 64 bytes
Try it online!
Rather than creating the output string from the start by greedily taking the largest part, this creates it from the end. For instance, the number of
I
's isn%5
, then the number ofV
's isn/5%2
, and so on. This is mixed base conversion with successive ratios of 5 and 2 alternating.Here's an iterative equivalent:
Python 2, 68 bytes
Try it online!
The
M
's need to be handled separately because any number of them could be present as there's no larger digit. So, after the other place values have been assigned, the value remaining is converted toM
's.For comparison, a greedy strategy (69 bytes):
Python 2, 69 bytes
Try it online!
The current digit value
d
is divided by either 2 or 5 to produce the next digit. The value ofd%3
tell us which one: ifd%3==1
, divide by2
; and ifd%3==2
, divide by 5.sumber
Mathematica, 81 bytes
Explicitly using the values and deriving the corresponding numerals seems to be one byte longer:
sumber
FromRomanNumeral@r
Excel,
236193161 bytes43 bytes saved thanks to @BradCAt this point, the answer really belongs totally to @BradC. Another 32 bytes saved.
Formatted:
sumber
CONCATENATE
with&
between each element, andQUOTIENT
withINT(A/B)
.REPT
already truncates the number if it isn't an integer, so you can save 30 more bytes by removing eachINT()
. Save 2 more by replacing both1000
with1E3
(although Excel doesn't seem to want to keep it that way once you hit enter).1E3
behaviour. Answer updated.Perl 5, 66 bytes
65 bytes of code +
-p
flag.Try it online!
Without changing the byte count,
MDCLXVI=~/./g
can be replaced byM,D,C,L,X,V,I
; and--$|?2:5
by$|--*3+2
.Much longer (99 bytes), there is:
sumber
CJam,
3528 bytes-7 bytes thanks to Martin Ender
Try it online!
Explanation
sumber
C#, 127 bytes
A purely hard coded ternary statement using recursion.
Full/Formatted version:
sumber
n>0
is justn
.int
can't be implicitly cast to abool
.05AB1E,
292625 bytesTry it online!
Explanation
sumber
JavaScript (ES6),
817569 BytesSaved 6 bytes thanks to @Neil for porting @Jörg Hülsermann's answer
Saved 6 bytes thanks to @Shaggy
Test cases:
Show code snippet
sumber
n%=x
within therepeat
method to save a few bytes.n=>'MDCLXVI'.replace(/./g,(c,i)=>c.repeat(n/a,n%=a,a/=i%2?5:2),a=1e3)
///, 50 bytes
Try it online!
Takes input in unary, and I'm (ab)using the footer field on TIO for input so output is preceded by a newline.
sumber
Python 3,
100 97 96 94 93 9190 bytesdef
; array as default parameter reduced an indentation space; unwanted variable declaration removeda%=
shorthand(a//i)
got removed[]
at the cost of one indentation space, thus saving 1 byte.Try it online!
sumber
a%=i
is a byte shorter :)b
as a variable within the function. That removes the necessity of brackets -b=1000,500,100,50,10,5,1
Cubix, 69
7480bytesTry it online!
Watch It Running
I have managed to compress it a bit more, but there is still some pesky no-ops, especially on the top face.
52"IVXLCDM"U
put the necessary divisors and characters on the stack. The 5 and 2 will be used to reduce the div/mod value and the characters will be discarded after use.UIN0/&0\&,/U
u-turns onto the top face and starts a long tour to get the input and push 1000 onto the stack. An initial divide is done and a u-turn onto ther
of the next snippet. This was an area I was looking at to make some savings.,r%ws;rr
beginning of the divmod loop. integer divide, rotate the result away mod, then rearrange top of stack to be reduced input, current divisor and divide result.3tus
bring the current character to the top and swap it with the divide result.!vsoUs(0;U
this is the print loop. while the div result is more than 0, swap with character output, swap back, decrement, push a 0 and drop it. On 0 redirect over the pop stack (remove divide result) and around the cube.\u;pwpsq,!@Urq;u
with a bit of redirection, this removes the character from the stack, brings the 5 and 2 to the top, swaps them and pushes one back down. The remaining is used to reduce the divisor. Halt if it reduces to 0, otherwise push the 5 or 2 to the bottom and re-enter the loop.sumber
Mathematica, 130 bytes
sumber
Python 2,
10990 bytesTry it online!
sumber
1000
can be1e3
(if you don't mind it being a float which shouldn't be a problem)float
, and you can't multiply a string by a float :cPHP, 70 bytes
Try it online!
sumber
T-SQL, 164 bytes
Line breaks added for readability only.
This version is a lot longer (230 characters), but feels much more "SQL-like":
Makes a table m with all the char-value mappings, and then loops through finding the largest value <= the number, concatenating the matching character.
sumber
Japt, 34 bytes
Test it online!
sumber
JavaScript (ES6), 65 bytes
A recursive function.
How?
The second recursive call
f(n-a)
really should bef(n-a,a)
. By omitting the 2nd parameter,a
andi
are reinitialized (to 1000 and 0 respectively) each time a new Roman digit is appended to the final result. This causes more recursion than needed but doesn't alter the outcome of the function and saves 2 bytes.Test cases
Show code snippet
sumber
J,
2623 bytes3 bytes saved thanks to Adám.
Try it online!
Similar to the APL answerbasically the same thing.sumber
#.inv
instead of#:
?#.inv
instead of#:
, since something like2 #: 4
is0
, whilst2 #.inv 4
is1 0 0
#
is/
;~
is⍨
;$
is⍴
;&
is∘
;#:
is⊤
. The only difference is that you use infinity_
while you could use0
like the APL answer.Batch, 164 bytes
Takes input on STDIN.
sumber
Oracle SQL, 456 bytes
Outputs:
Please note the actual size of the line is 460bytes, because it includes the input number (2849).
Ungolfed:
How it works: I calculate how many of each letter I need, by calculating the most I can get to with the higher value one (infinity for M), and then doing an integer division between the current letter's value and the result of that.
E.g. 2348, how many
C
s do I need?trunc((2348-mod(2348,500))/100)
= 3.Then, I
listagg
that letter together 3 times (exploitingCONNECT BY
to generate the 3 rows I need). Finally, Ilistagg
everything together.Kinda bulky, but most of it is the
select from dual
s in the conversion table and I can't really do much about that...sumber
Java (OpenJDK 8),
119118 bytesTry it online!
Saved a byte thanks to @TheLethalCoder
sumber
v
andi
in the first for loop to save a byte?Charcoal,
61 5046 bytesTry it online!
Explanation:
sumber
Nν
is one byte shorter thanANν
,¬‹
is one byte shorter than subtracting 1, and if you use÷
(IntDivide) instead of∕
(Divide) then you can useφ
as the outer loop condition. However, I think you can get it down to 40 bytes by looping overMDCLXVI
directly instead.C++, 272 Bytes
sumber
C, 183 Bytes
Same algorithm as before, just using plain c arrays instead of an std::map, partially inspired by @xnor's answer and using a string to store the letters.
sumber
Common Lisp, 113 bytes
This is an anonymous function, returning the result as a string.
Ungolfed, with descriptive variable names and comments:
CL has built-in Roman numeral formatter. Sadly it doesn't work for numbers larger than 3999.
sumber
Charcoal, 34 bytes
Originally based on @CarlosAlego's answer. A port of @xnor's Python solution is also 34 bytes:
Edit: A port of @xnor's other Python solution turns out to be 33 bytes!
Try it online! Link is to verbose version of code. Note that I've used
⁺׳﹪φ³±¹
instead of⁻׳﹪φ³¦¹
because the deverbosifier is currently failing to insert the separator.sumber