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 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 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 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 |
|||
08-08-2021, 12:45 PM
Post: #2
|
|||
|
|||
RE: Advanced String Programs (41CX, DM41X, Extended Module Needed) | |||
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 Updated ASAMP: Code: 01 LBL^T ASAMP Updated download (first link does not work): https://drive.google.com/file/d/1UD8CAIL...sp=sharing |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)