Post Reply 
checkdigit calculation for HP-17b
07-28-2015, 06:22 PM
Post: #16
RE: checkdigit calculation for HP-17b
(07-28-2015 02:54 PM)Don Shepherd Wrote:  I finally figured out exactly how your method works.

This is a step-by-step transformation of your formula to my solution.

1. We can remove the inner calculation of MOD 10 as it's enough to do that once:

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


2. We can remove multiples of 10 as that doesn't change the result when calculating MOD 10:

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


3. We can replace -9 by +1 as they are the same MOD 10:

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


4. We can move the - sign into sum.

CD = MOD(-
Σ(I:0:LOG(N):1:
L(A:MOD(IDIV(N:10^I):10)×(2-MOD(I:2)))
+IDIV(G(A):10)
):10)

Here comes the tricky part. If we move that just to the summand we have to change the sign of both expressions:

CD = MOD(
Σ(I:0:LOG(N):1:
-L(A:MOD(IDIV(N:10^I):10)×(2-MOD(I:2)))
-IDIV(G(A):10)
):10)

But if we move it into the Let-A expression the sign of A will change and thus we don't have to change the sign of the Get-A expression as well:

CD = MOD(
Σ(I:0:LOG(N):1:
L(A:-MOD(IDIV(N:10^I):10)×(2-MOD(I:2)))
+IDIV(G(A):10)
):10)


5. Now we're nearly there. Just move the - sign to the 2nd factor:

CD = MOD(
Σ(I:0:LOG(N):1:
L(A:MOD(IDIV(N:10^I):10)×(-2+MOD(I:2)))
+IDIV(G(A):10)
):10)


6. Change the order and we're finally done:

CD = MOD(
Σ(I:0:LOG(N):1:
L(A:MOD(IDIV(N:10^I):10)×(MOD(I:2)-2))
+IDIV(G(A):10)
):10)

For further information I recommend this article on modular arithmetic.
BTW: That's why casting out nines works.

Your table looks familiar: I often have to do these kind of calculations until I understand something.

Kind regards
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-28-2015 06:22 PM



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