Post Reply 
Advanced String Programs (41CX, DM41X, Extended Module Needed)
08-07-2021, 11:58 PM
Post: #1
Advanced String Programs (41CX, DM41X, Extended Module Needed)
Introduction and Generating Random Integers

This blog entry features three programs for the Swiss Micros DM41X and HP 41CX (or a HP 41C with an extended module):

ACODE: generate a code of random letters

ASAMP: generate a sample of numbers of digits 0 to 9, where no numbers repeat. ASAMP accepts up to 9 numbers.

CRYPT: allows the user to "add" or "subtract" a code to a word to encrypt it. CRYPT is a basic form of encryption.

Both ACODE and ASAMP use a random number generator. Unfortunately, the HP 41C does not have a random number generator and one must be programmed. I wanted to avoid reinventing the wheel. The following formula, obtained from the book An Atlas of Functions (see source), generates random numbers of value 0 ≤ r < 1:

r_n+1 = [ ( (4561 * int(243000 * r_n) + 51349 ) mod 243000 ] ÷ 243000

To convert this into a random integer from a to b:

randint = round( (b - a + 1) * r, 0) + a = int( (b - a + 1) * r + 1/2 ) + a

int is the integer function. The second formula is useful because we don't have to change display modes to execute the calculation.

Note:

XTOA takes an integer from the stack and appends the associated code to the alpha string.

ATOX takes the left most character, converts it to code, and deposits it on the X stack. The length of the alpha string is reduced by one.

Codes:
Alphabet: 65 is code for A, 90 is code for Z (all letters inclusive)
Numbers: 48 is code for 0, 57 is code for 57 (all letters inclusive)
# 35
$ 36
% 37
& 38
: 58
@ 64
[ 91
] 93
Σ 126

The system's DATE and TIME are used to generate an initial seed

Swiss Micros DM41X Program: ACODE

Instructions:
Enter the length, execute ACODE

Example (results will vary):
10 ACODE (may) return CDSJHPNWLD (10 letters)
11 ACODE -> NVJZJMVVBSV

Code:
01 LBL^T ACODE
02 CLA
03 STO 01
04 DATE
05 TIME
06 +
07 2
08 /
09 FRC
10 STO 02
11 LBL 00
12 RCL 02
13 XEQ 01
14 XEQ 02
15 XTOA
16 DSE 01
17 GTO 00
18 AVIEW
19 RTN
20 LBL 01
21 RCL 02
22 243 E3
23 *
24 LASTX
25 X<>Y
26 INT
27 4561
28 *
29 51349
30 +
31 X<>Y
32 MOD
33 LASTX
34 /
35 STO 02
36 RTN
37 LBL 02
38 26
39 *
40 2 
41 1/X
42 +
43 INT
44 65
45 +
46 RTN
47 END

Swiss Micros DM41X Program: ASAMP

Instructions:
Enter the length, execute ASAMP

If the length is greater than 9, an error is generated.

Example (results will vary):
4 ASAMP can generate results such as 4381, 0361, 4920
7 ASAMP can generate results such as 6732145, 9852067, 1963542

Results are returned as an alpha string

Code:
01 LBL^T ASAMP
02 CLA
03 STO 01
04 9
05 X<Y?
06 GTO 03
07 DATE
08 TIME
09 *
10 FRC
11 STO 02
12 LBL 00
13 XEQ 01
14 STO 03
15 POSA
16 -1
17 X=Y?
18 GTO 02
19 GTO 00
20 LBL 02
21 RCL 03
22 XTOA 
23 DSE 01
24 GTO 00
25 AVIEW
26 RTN
27 LBL 01
28 RCL 02
29 243 E3
30 *
31 LASTX
32 X<>Y
33 INT
34 4561
35 *
36 51349
37 +
38 X<>Y
39 MOD
40 LASTX
41 / 
42 STO 02
43 10
44 *
45 2
46 1/X
47 +
48 INT
49 48
50 +
51 RTN
52 LBL 03
53 0
54 1/X
55 RTN
56 END

Swiss Micros DM41X Program: CRYPT

Syntax:
Store your word in the Alpha register
Give a key (integer), can be positive or negative
XEQ CRYPT

Example:
Starting alpha string: MATHS
10 CRYPT returns WKDRC
-10 CRYPT returns MATHS (where you started from)
This allows for two people to have short encoded messages and a secret key.

Code:
01 LBL^T CRYPT
02 STO 01
03 ALENG
04 STO 02
05 LBL 00
06 ATOX
07 65
08 - 
09 RCL 01
10 +
11 26
12 MOD
13 65
14 + 
15 XTOA
16 DSE 02
17 GTO 00
18 AVIEW
19 RTN

You can download all three files (in .raw format) here: https://drive.google.com/file/d/1ll52VAu...sp=sharing


Source for the Random Number Formula:

Keith Oldham, Jan Mayland, Jerome Spainer An Atlas of Functions 2nd Edition Springer: New York, NY. 2009. ISBN 9780387488066
Visit this user's website Find all posts by this user
Quote this message in a reply
08-08-2021, 12:45 PM
Post: #2
RE: Advanced String Programs (41CX, DM41X, Extended Module Needed)
(08-07-2021 11:58 PM)Eddie W. Shore Wrote:  To convert this into a random integer from a to b:

randint = round( (b - a + 1) * r, 0) + a = int( (b - a + 1) * r + 1/2 ) + a

This is wrong, giving possible randint of b+1. We should drop the 1/2

0 ≤ r < 1
a ≤ (b-a+1)*r+a < b+1
Find all posts by this user
Quote this message in a reply
08-08-2021, 08:30 PM
Post: #3
RE: Advanced String Programs (41CX, DM41X, Extended Module Needed)
Thank you, Albert. I am going to use the formula:

randint = int((b - a + 1) * r) + a

This saves three program steps from ACODE and ASAMP.

Updated ACODE:
Code:
01 LBL^T ACODE
02 CLA
03 STO 01
04 DATE
05 TIME
06 +
07 2
08 /
09 FRC
10 STO 02
11 LBL 00
12 RCL 02
13 XEQ 01
14 XEQ 02
15 XTOA
16 DSE 01
17 GTO 00
18 AVIEW
19 RTN
20 LBL 01
21 RCL 02
22 243 E3
23 *
24 LASTX
25 X<>Y
26 INT
27 4561
28 *
29 51349
30 +
31 X<>Y
32 MOD
33 LASTX
34 /
35 STO 02
36 RTN
37 LBL 02
38 26
39 *
40 INT
41 65
42 +
43 RTN
44   END

Updated ASAMP:
Code:
01 LBL^T ASAMP
02 CLA
03 STO 01
04 9
05 X<Y?
06 GTO 03
07 DATE
08 TIME
09 *
10 FRC
11 STO 02
12 LBL 00
13 XEQ 01
14 STO 03
15 POSA
16 -1
17 X=Y?
18 GTO 02
19 GTO 00
20 LBL 02
21 RCL 03
22 XTOA 
23 DSE 01
24 GTO 00
25 AVIEW
26 RTN
27 LBL 01
28 RCL 02
29 243 E3
30 *
31 LASTX
32 X<>Y
33 INT
34 4561
35 *
36 51349
37 +
38 X<>Y
39 MOD
40 LASTX
41 / 
42 STO 02
43 10
44 *
45 INT
46 48
47 +
48 RTN
49 LBL 03
50 0
51 1/X
52 RTN
53 END

Updated download (first link does not work):
https://drive.google.com/file/d/1UD8CAIL...sp=sharing
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)