Post Reply 
(12C) Luhn algorithm
04-23-2018, 08:19 AM (This post was last modified: 04-23-2018 08:56 AM by Dieter.)
Post: #2
RE: (12C) Luhn algorithm
(04-18-2018 06:29 PM)pinkman Wrote:  referring to the wikipedia page of the Luhn algorithm, I propose an implementation for the 12c.
...

Looking forward to reading you.

Another example of putting the 12C's limited programming capabilites to good use. ;-)

Here is a significantly shorter version that takes advantage of two simple tricks that can also be useful for other applications:

1. In your program the change between odd and even digit positions is done by checking if the index is divisible by 2. Afterwards the digit is doubled or not. My program toggles R1 between 0 and 2 (via R1 := 2 – R1). If it's zero the doubling is skipped, otherwise the factor 2 is already in X.

2. The original program calculates the digit sum of a two-digit result in a very straightforward way: divide x by 10, take the integer part, calculate the remainder, and add both. But since x does not exceed 18 the digit sum can also be calculated by subtracting 9 if x exceeds 9. This was implemented in the HP41 program in the General Forum's Luhn thread. Here the code sequence was

Code:
...
2
x
9
X>Y?
CLX
-
...

This means: if after doubling x is less than 9 (i.e. 0, 2, 4, 6 or 8) don't subtract anything (CLX –), and if it is ≥ 9 (i.e. 10, 12, 14, 16 or 18) subtract 9 to get 1, 3, 5, 7 or 9 as the digit sum.

Now HP's lower end programmables do not feature an X>Y? test. X≤Y? and X=0? is all they have. But there is a way. If one test is followed by another one which always tests false (!), the first test command is inverted! So an X≤Y? followed by an X=0? in effect is an X>Y? as long as X is not zero. Here X always is 9, so X=0? always tests false and we get an X>Y? test as desired.

Here is the complete program:

Code:
01 1
02 0
03 ÷
04 INT
05 LSTX
06 FRAC
07 1
08 0
09 x
10 2
11 RCL 1
12 -
13 STO 1
14 X=0?
15 GTO 23
16 x
17 9
18 X≤Y?
19 X=0?
20 CLX
21 -
22 ENTER
23 R↓
24 STO+0
25 R↓
26 GTO 00

Initialize with
0  STO 0
2  STO 1

With two additional steps the program can be modified so that the initialization can be done with
0  ST0 0  STO 1
or even a simple CLEAR REG.

If you like this better here is an alternate version:

Code:
01 1
02 0
03 ÷
04 INT
05 LSTX
06 FRAC
07 1
08 0
09 x
10 RCL 1
11 X=0?
12 GTO 20
13 x
14 9
15 X≤Y?
16 X=0?
17 CLX
18 -
19 ENTER
20 R↓
21 STO+0
22 2
23 RCL 1
24 -
25 STO 1
26 R↓
27 R↓
28 GTO 00

In either case then proceed as with the original program.
Note: the number of processed digits here is not stored in R1.

Hint: INT LSTX FRAC is one step shorter than ENTER INT X<>Y FRAC. ;-)
Also note the dummy ENTER in line 22 or 20 that neutralizes the following R↓.
Yes, a GTO would have done it just as well.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(12C) Luhn algorithm - pinkman - 04-18-2018, 06:29 PM
RE: (12C) Luhn algorithm - Dieter - 04-23-2018 08:19 AM
RE: (12C) Luhn algorithm - pinkman - 04-24-2018, 08:00 AM
RE: (12C) Luhn algorithm - Dieter - 04-24-2018, 06:35 PM
RE: (12C) Luhn algorithm - Dieter - 04-24-2018, 07:40 PM
RE: (12C) Luhn algorithm - pinkman - 04-24-2018, 08:27 PM
RE: (12C) Luhn algorithm - Dieter - 04-25-2018, 07:22 AM
RE: (12C) Luhn algorithm - pinkman - 04-25-2018, 02:52 PM



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