Following up on LogicalJoe's challenge, this is the reverse operation.

Write a program that takes an integer 1 ≤ N ≤ 3999 from Ans, and converts it to Roman Numerals, giving the output as a string in Ans. Invalid inputs may produce undefined behavior.

Your byte count is the number of bytes of code, i.e. (program size) - 9 - (name length).

Reference implementation (172 bytes):

Code:
```Ans->N ".->Str1 "IIIVIIIX XXXLXXXC CCCDCCCM MMM->Str3 {1,1,1,3,4,4,4,4,7->|LSTART {1,2,3,2,1,2,3,4,2->|LLEN For(I,int(log(N)),0,~1    int(10fPart(N/10^(I+1->D    If D    Str1+sub(Str3,9I+|LSTART(D),|LLEN(D->Str1 End sub(Str1,2,length(Str1)-1```

Test cases:

Code:
```1 -> I 78->LXXVIII 404 -> CDIV 999 -> CMXCIX 1000 -> M 2019 -> MMXIX 3456 -> MMMCDLVI```

Note that your program must produce results for all 1 ≤ N ≤ 3999, not just the given test cases.

I will post my answer when someone beats it, or after a week or so. At the start of the contest, it's 96 bytes.

Results

lirtosiast/PT_/Mateo: 85
lirtosiast: 90
PT_/Mateo: 93
Weregoose: 108
Here's my 108 bytes, which I am happy with:

Ans→N:{1,4,5,9
augment(Ans,10augment(Ans,10augment(Ans,{10→N
".→Str1
While N
sum(N≥ʟN→X
Str1+sub("IIVIXXLXCCDCM",Ans,1+not(fPart(.5Ans→Str1
N-ʟN(X→N
End
sub(Str1,2,length(Str1)-1
I got it down to only 93 bytes, with a little help from Mateo:

Ans->N
"
While N
Ans->Str0
timeCnv(101109
augment(Ans,10Ans
augment(Ans,|E2Ans->X
sum(N>=Ans->P
N-|LX(Ans->N
sub("IIVIXXLXCCDCM",P,gcd(P,2
If length(Str0
Str0+Ans
End
PT_ wrote:
timeCnv(101109

You know timeCnv( is a 2-byte token...
You don't save any bytes over just doing this:

Code:
`{1,4,5,9`

you do get extra points for severe obfuscation though
Since PT_ and Mateo achieved 93, here's my 96 byte answer:

Code:
```Ans->N "IVXLCDM While N    Ans->Str1    int(log(N->E    int(N/10^(Ans->D    D>=5 and D<9->V    N-10^(E)5^Vcos(pimax(D={4,9->N    Str1+sub(Str1,2E+V+1,1 End sub(Ans,8,length(Ans)-7```

Edit: I achieved 90 bytes after the hint about PT_ using the list {1,4,5,6}.

Code:
```Ans->N "IXCMVLD While N    Ans->Str1    10^(int(log(N->E    sum(N>=E{4,5,9->D    5^(D=2->V    N-EVcos(piD->N    Str1+sub(Str1,log(E)+V,1 End sub(Ans,8,length(Ans)-7```
I need to get better at these. 119 bytes:

:Ans->Z:".->Str1
:For(I,-4,-1
:4^^-1int(10fPart(Z10^^(I->D
:not(fPart(Ans
:If D
:Str1+sub("IIIVIIIXXXLXXXCCCDCCCMMM",3int(D)-7I-6+Ans-2DAns,(D>2)+2DAns+4fPart(D->Str1
:End
:sub(Str1,2,length(Str1)-1
mr womp womp wrote:
PT_ wrote:
timeCnv(101109

You know timeCnv( is a 2-byte token...
You don't save any bytes over just doing this:

Code:
`{1,4,5,9`

you do get extra points for severe obfuscation though

I do know that, but hey, timeCnv is just more fancy
Maybe it's faster. Because I think some tokens are faster than others.

Edit: I wasn't thinking; . . . it's a byte-saving challenge

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are GMT - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum