While looking through Stack Exchange I found a two-way number to Roman Numeral converter by a user named FlyAwayBirdie: http://codereview.stackexchange.com/questions/79734/ti-basic-roman-numeral-converter:
Code:
The routine to convert a number to a Roman numeral is probably solid (it was written by Ed H in the routines thread) but the other way looks like it has some room for optimization. Here are the optimizations I have. My code subtracts a letter any time the next letter has greater value, so it will work for some invalid Roman numberals like VL="45" and XIC="109":
Code:
Can anyone go even smaller?
By the way, I found a bug with seq(: if you have both u and u(X, with X an expression, inside a seq( loop with n as the index, all occurences of u after the u(X) in the body of the seq( loop will be evaulated as u(X+1) rather than u(n). Or something like that; I don't know why or exactly what it does.
EDIT: Optimized. This is not the fastest possible because u is recomputed every time, but it is fast enough.
Code:
DelVar IDelVar VDelVar XDelVar LDelVar CDelVar DDelVar M"?->Str2
"IVXLCDM->Str3
ClrHome
Input Str1
expr(Str1->N
If N
Then
For(I,0,log(N
10fPart(.1N->A
While A
sum({4.1,4.1,9.2}(A={4,5,9->B
sub(Str3,2I+10fPart(B)+1,1)+Str2->Str2
abs(A-1-int(B->A
End
int(.1N->N
End
sub(Str2,1,length(Str2)-1
Else
Str2+Str1+"?->Str1
For(I,0,5
inString(Str1,sub("IVIXXLXCCDCM",2I+1,2
If Ans
Then
sub(Str1,1,Ans-1)+sub(Str1,Ans+2,length(Str1)-Ans-1->Str1
C+(4+10fPart(I/2))10^(iPart(I/2->C
End
End
For(I,1,length(Str1
inString(Str3,sub(Str1,I,1
If Ans
C+(5-8fPart(.5Ans))10^(iPart(.5Ans-.5->C
End
C
End
Ans
The routine to convert a number to a Roman numeral is probably solid (it was written by Ed H in the routines thread) but the other way looks like it has some room for optimization. Here are the optimizations I have. My code subtracts a letter any time the next letter has greater value, so it will work for some invalid Roman numberals like VL="45" and XIC="109":
Code:
"IVXLCDM->Str3
ClrHome
Input Str1
expr("0"+Str1->N
"?->Str2
If N
Then
//Ed H's routine
Else
"inString(Str3,sub(Ans,n,1"->u //doesn't change Ans
Str1+Ans
sum(seq(iPart((.5+fPart(.5u))10^(int(.5u))(1-2(u<u(n+1)))),n,1,length(Str1 //"?" has a value of zero, so the last letter will always be added
End
Ans
Can anyone go even smaller?
By the way, I found a bug with seq(: if you have both u and u(X, with X an expression, inside a seq( loop with n as the index, all occurences of u after the u(X) in the body of the seq( loop will be evaulated as u(X+1) rather than u(n). Or something like that; I don't know why or exactly what it does.
EDIT: Optimized. This is not the fastest possible because u is recomputed every time, but it is fast enough.