Convert integer to roman numerals

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
Jvi
Posts: 12
Joined: Fri Aug 09, 2019 6:03 am
Location: Greve Denmark

Convert integer to roman numerals

Post by Jvi »

This program converts the integer in register X to roman numerals.
The roman numerals are M:1000, D:500, C:100, L:50, X:10, V:5 and I:1.
You make a roman numeral by writing the letters in descending order, adding the values.
If a Letter with a lower value is placed in front of a letter with a higher value, the lower value is subtracted

Example: 24 = XXIV : 10 + 10 - 1 + 5

Interesting numbers to try:
1444
1666
1888

Code: Select all

00 { 181-Byte Prgm }
01▸LBL "ROMAN"
02 STO 00
03 CLA
04 AIP
05 ├" = "
06 1000.77
07 XEQ 00
08 900.6777
09 XEQ 00
10 500.68
11 XEQ 00
12 400.6768
13 XEQ 00
14 100.67
15 XEQ 00
16 90.8867
17 XEQ 00
18 50.76
19 XEQ 00
20 40.8876
21 XEQ 00
22 10.88
23 XEQ 00
24 9.7388
25 XEQ 00
26 5.86
27 XEQ 00
28 4.7386
29 XEQ 00
30 1.73
31 XEQ 00
32 CLST
33 AVIEW
34 RTN
35▸LBL 00
36 ENTER
37 IP
38 STO 01
39 X<>Y
40 FP
41 STO 02
42▸LBL 01
43 RCL 00
44 RCL 01
45 X>Y?
46 RTN
47 STO- 00
48 RCL 02
49▸LBL 02
50 100
51 ×
52 XTOA
53 FP
54 X≠0?
55 GTO 02
56 GTO 01
57 END
ROMAN.raw
(184 Bytes) Downloaded 175 times
Nissen
Posts: 18
Joined: Wed Jun 02, 2021 6:01 pm
Location: Copenhagen

Re: Convert integer to roman numerals

Post by Nissen »

Yes, it works!
Do you have a program converting roman numerals to integers?
Regards
Nissen
Jvi
Posts: 12
Joined: Fri Aug 09, 2019 6:03 am
Location: Greve Denmark

Re: Convert integer to roman numerals

Post by Jvi »

Hello Nissen

Not at the moment. If i make one, I will post it on this forum
Peet
Posts: 256
Joined: Tue Sep 29, 2020 12:01 am
Location: Germany

Re: Convert integer to roman numerals

Post by Peet »

Nissen wrote:
Wed Jul 28, 2021 4:05 pm
Do you have a program converting roman numerals to integers?
A version for the HP41CX (which should also run on the 42) can be found in the Original HP-41C Software Library on MoHPC.
My programmable calculators - former: CBM PR100, HP41CV, HP28S, HP11C - current: HP48G(256kB), HP35S, Prime, DM41X, DM42
whuyse
Posts: 197
Joined: Thu Dec 21, 2017 1:23 pm

Re: Convert integer to roman numerals

Post by whuyse »

1. use a 13x2 matrix "RN"
[[ 1000 77 ]
[ 900 "CM" ]
[ 500 68 ]
[ 400 "CD" ]
[ 100 67 ]
[ 90 "XC" ]
[ 50 76 ]
[ 40 "XL" ]
[ 10 88 ]
[ 9 "IX" ]
[ 5 86 ]
[ 4 "IV" ]
[ 1 73 ]]
(the numbers 77 etc in the second column make it slightly easier to enter; they can be replaced by their counterparts "M", "D", "C" etc.)

2. Then, these two programs will do the conversions:

Code: Select all

00 { 81-Byte Prgm }
01▸LBL "→ROM"
02 ENTER
03 EDITN "RN"
04▸LBL 02
05 X>Y?
06 GTO 00
07 STO- ST Y
08 →
09 XTOA
10 ←
11 GTO 02
12▸LBL 00
13 ↓
14 FC? 76
15 GTO 02
16 EXITALL
17 RTN

18▸LBL "→ARB"
19 INDEX "RN"
20 ALENG
21 FP
22 ENTER @ L X Y Z T
23▸LBL 03 @ count prev sum
24 ATOX
25 XTOA
26 [FIND]
27 X=0? @ always false filler
28 ASTO ST Z @ will produce an 'Alpha Data is invalid' error
29 J-
30 CLX
31 RCLEL
32 X<>Y
33 X<Y?
34 +/-
35 STO+ ST Z
36 R↓
37 DSE ST L
38 GTO 03
39 EXITALL
40 +
41 END
Cheers, Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
Jvi
Posts: 12
Joined: Fri Aug 09, 2019 6:03 am
Location: Greve Denmark

Re: Convert integer to roman numerals

Post by Jvi »

Here is my version of a program, which can convert a roman numeral to an integer.
You write the roman numeral in the alpha register, and start the program. The converted number is shown in the X register.
It converts all the regular roman numerals, but it will also happily convert all the 'Irregular' Roman numerals like MIM or XMMV

Code: Select all

00 { 128-Byte Prgm }
01▸LBL "R->N"
02 CLRG
03 XEQ 10
04▸LBL 00
05 ALENG
06 X=0?
07 GTO 01
08 RCL 02
09 STO 01
10 XEQ 10
11 RCL 02
12 RCL 01
13 X<Y?
14 +/-
15 STO+ 00
16 GTO 00
17▸LBL 01
18 RCL 02
19 STO+ 00
20 CLST
21 RCL 00
22 RTN
23▸LBL 10
24 ATOX
25 STO 03
26 0
27 STO 02
28 1000.77
29 XEQ 11
30 500.68
31 XEQ 11
32 100.67
33 XEQ 11
34 50.76
35 XEQ 11
36 10.88
37 XEQ 11
38 5.86
39 XEQ 11
40 1.73
41 XEQ 11
42 RTN
43▸LBL 11
44 ENTER
45 IP
46 X<>Y
47 FP
48 100
49 ×
50 RCL 03
51 X≠Y?
52 RTN
53 R↓
54 R↓
55 STO 02
56 RTN
57 END
rom2n.raw
(131 Bytes) Downloaded 157 times
Post Reply