Post Reply 
(12C) Luhn algorithm
04-24-2018, 06:35 PM (This post was last modified: 04-24-2018 08:12 PM by Dieter.)
Post: #4
RE: (12C) Luhn algorithm
(04-24-2018 08:00 AM)pinkman Wrote:  I did not want to use a toggle to identify the odd/even case, because of the advantage of having a counter of digits processed. I found a new solution based on the deterministic result of dividing by 2 any integer: 0 or 0.5. Thus multiplying by 2 and adding 1 gives a factor suitable for any digit to process, without using a test.

First of all: you can save two steps if you have the "1" in line 16 followed by a STO+1. This way the counter is incremented en passant and you can delete the three steps near the end that do it now.

Regarding the method of calculating a factor (1 or 2) from the R1 counter: I had a similar idea, and avoiding tests and GTOs is something I really like. In this case it could even be done with two steps less:

Code:
...
RCL 1
2
/
FRAC
X=0?
e^x
/

The e^x turns a 0 into 1, so the digit is divided by either 0,5 (=doubled) or 1 (unchanged).

But... sometimes even good ideas don't work. All this would be fine, even with four steps saved, IF the 12C had an X<Y? test as originally shown in your listing (I now see you corrected this). But in fact the 12C features an X≤Y? test. This way the digit 9 will not get processed correctly. Since 9 ≤ 9 the test is true, so X=0? is also tested, which is false, so the CLX is not executed and the result is zero (9 – 9). So this method is not an option here.

HP25 users: this is your moment! You can implement the idea above with the following code sequence, since your calculator sports an X≥Y? test:

Code:
RCL 1
2
/         // divide digit position in R1 by 2
FRC       // fractional part is 0 for even or 0,5 for odd digit position
X=0?      // if even...
e^x       // ...turn zero into 1, else keep the 0,5
/         // divide digit by 0,5 or 1, i.e. multiply by 2 or 1
9
X≥Y?      // if result ≤ 9
CLX       // leave it as it is (subtract 0)
-         // else subtract 9 to get the digit sum

Note: be sure to add an ENTER as the first line, or even better something useful like FIX 0. If you don't the following 10 may be appended to your input. This is one of the HP25's ..."special features".

Owners of HP calculators with the common eight test commands may replace the X≥Y? with an X≠Y? X>Y? sequence. Yes, that's another trick from the olden days... ;-)

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)