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
 Don Shepherd Senior Member Posts: 749 Joined: Dec 2013
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.
 « Next Oldest | Next Newest »

 Messages In This Thread checkdigit calculation for HP-17b - Don Shepherd - 07-26-2015 12:08 AM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-26-2015, 07:47 AM RE: checkdigit calculation for HP-17b - Don Shepherd - 07-26-2015, 12:56 PM RE: checkdigit calculation for HP-17b - Don Shepherd - 07-28-2015, 02:54 PM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-26-2015, 01:25 PM RE: checkdigit calculation for HP-17b - Don Shepherd - 07-26-2015, 02:04 PM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-26-2015, 04:45 PM RE: checkdigit calculation for HP-17b - Don Shepherd - 07-26-2015, 05:02 PM RE: checkdigit calculation for HP-17b - wynen - 07-27-2015, 04:11 PM RE: checkdigit calculation for HP-17b - Gerson W. Barbosa - 07-26-2015, 07:29 PM RE: checkdigit calculation for HP-17b - Don Shepherd - 07-26-2015, 08:34 PM RE: checkdigit calculation for HP-17b - Gerson W. Barbosa - 07-28-2015, 06:48 PM RE: checkdigit calculation for HP-17b - Gerson W. Barbosa - 07-26-2015, 11:03 PM RE: checkdigit calculation for HP-17b - Don Shepherd - 07-26-2015, 11:42 PM RE: checkdigit calculation for HP-17b - Bill (Smithville NJ) - 07-27-2015, 01:03 AM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-28-2015, 06:16 PM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-28-2015, 06:22 PM RE: checkdigit calculation for HP-17b - Don Shepherd - 07-28-2015, 10:11 PM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-28-2015, 08:28 PM RE: checkdigit calculation for HP-17b - Gerson W. Barbosa - 07-28-2015, 09:51 PM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-28-2015, 09:44 PM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-29-2015, 01:52 AM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-29-2015, 02:20 AM RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-29-2015, 08:30 AM

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