Post Reply 
checkdigit calculation for HP-17b
07-26-2015, 12:08 AM (This post was last modified: 07-26-2015 02:52 PM by Don Shepherd.)
Post: #1
checkdigit calculation for HP-17b
The Luhn algorithm for calculating a standard mod-10 checkdigit for an account or ID number lends itself very well to a simple program, or in this case a 17b solver equation.

Checkdigits are typically used during data entry to prevent the transposition of digits, so that ID 12345, if keyed in as 13245, can be corrected before the erroneous data becomes part of a file. Each digit in the account number, starting at the right-hand side, is multiplied by the pattern 212121..., the digits of the products are summed and the total is subtracted from the next higher multiple of 10. The result is the checkdigit associated with the ID number, the rightmost digit.

An example:

ID: 4 5 6 7 8 9
multiply: 1 2 1 2 1 2
product: 4 10 6 14 8 18
digits sum: 33
subtract from next higher multiple of 10: 40-33=7
checkdigit is 7, so the final ID is 4567897

In 2007, when I became aware of the 17b/17bii, I wrote a solver equation to calculate the checkdigit of an ID number, up to 12 digits in length. Here is that equation:

CD = 0\(\times\)L(M:2)\(\times\)L(C:-1)
+MOD(10-
MOD(\(\Sigma\)(I:0:LOG(N):1:
0\(\times\)L(A:MOD(IP(N):10)\(\times\)G(M))
+IF(G(A)<10:G(A):G(A)-9)
+0\(\times\)L(M:G(M)+G(C))
\(\times\)L(C:-G(C))
\(\times\)L(N:N\(\div\)10)):10):10)

That equation works, but it is rather long, and smaller solver equations run faster than longer ones, so recently I took another look at this equation, with the following goals:

  1. Make equation as small as possible
  2. Avoid IF functions
  3. Avoid 0 times
  4. Handle getting each digit of the input number
  5. Handle the 2121 multiplier
  6. Handle 2X7 =14, you really want 5 not 14
  7. Handle checkdigit 0, because 10-0=10, not 0


With those goals in mind, here is what I came up with:

CD = MOD(10-
MOD(\(\Sigma\)(I:0:LOG(N):1:
L(A:MOD(IDIV(N:10^I):10)\(\times\)(2-MOD(I:2)))
-9\(\times\)IDIV(G(A):10)
):10):10)

This reduced the size of the equation from 164 characters to 92 characters, a 44% reduction in size, and I achieved all of my goals. And, more importantly, it forced me to think through exactly what was needed, and use the available 17b solver functions to achieve this.

The 17b is a great little machine and can exercise your mind in 2015 as well as it could in 1988.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
checkdigit calculation for HP-17b - Don Shepherd - 07-26-2015 12:08 AM



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