Post Reply 
checkdigit calculation for HP-17b
07-28-2015, 06:48 PM
Post: #17
RE: checkdigit calculation for HP-17b
Quoting myself:

(07-26-2015 07:29 PM)Gerson W. Barbosa Wrote:  C=10*MOD(MOD(0*L(T:0)+Σ(I:1:9:1:0*L(T:G(T)+I*MOD(N:10))+(10-I)
*MOD(N:10)+0*L(N:IDIV(N:10))):11):10)+MOD(MOD(G(T):11):10)
...

123 456 789 --> 0 (they should be 00, actually)
976 431 258 --> 64
777 777 777 --> 77

I found out my first example was wrong, it should be 09, not 00. The algorithm I use is different from the official one [ function TestaCPF(strCPF) ], but I've managed to fix it so that it always gives the same results:

C=10*MOD(MOD(0*L(T:0)+L(S:Σ(I:1:9:1:0*L(T:G(T)+I*MOD(N:10))+(10-I)*MOD(N:10)
+0*L(N:IDIV(N:10)))):11):10)+MOD(MOD(G(T)+9*IDIV((MOD(G(S):11)):10):11):10)


123 456 789 --> 9 (09 actually, since we have two checking digits)
976 431 258 --> 64
777 777 777 --> 77

Here are the equivalent Free Pascal code and results:

Code:

-------------

Program CPF;
Uses Crt;
var d1,d2, i: Byte;
    s, t: Integer;
       n: Longint;
begin
  ClrScr;
  Read(n);
  s:=0;
  t:=0;
  for i:=1 to 9 do
    begin
      s:=s+(10-i)*(n Mod 10);
      t:=t+i*(n Mod 10);
      n:=n div 10
    end;
  t:=t+9*((s Mod 11) div 10);
  d1:=(s Mod 11) Mod 10;
  d2:=(t Mod 11) Mod 10;
  GotoXY(10,1);
  WriteLn('-',d1:1,d2:1)
end.

-------------

123456789-09
976431258-64
777777777-77

------------

It intrigues me a bit why this more simple algorithm works, but I am not nearly as diligent as Don to find the reason.

Code:

976 431 258 

Algorithm 1:

   9    7    6    4    3    1    2    5    8 

x  1    2    3    4    5    6    7    8    9

   9 + 14 + 18 + 16 + 15 +  6 + 14 + 40 + 72 = 204; 204 mod 11 = 6; 1st Chk Dgt


   9    7    6    4    3    1    2    5    8 

x  9    8    7    6    5    4    3    2    1

  81 + 56 + 42 + 24 + 15 +  4 +  6 + 10 +  8 = 246; 246 mod 11 = 4; 2nd Chk Dgt


Algorithm 2:
  
   9    7    6    4    3    1    2    5    8 

x 10    9    8    7    6    5    4    3    2 

  90 + 63 + 48 + 28 + 18 +  5 +  8 + 15 + 16 = 291; 291 x 10 = 2910; 2910 mod 11 = 6; 1st Chk Dgt

                                                                                   |
                                                +----------------------------------+
                                                |
                                                v

   9    7    6    4    3    1    2    5    8    6

x 11   10    9    8    7    6    5    4    3    2

  99 + 70 + 54 + 32 + 21 +  6 + 10 + 20 + 24 + 12 = 348; 348 x 10 = 3480; 3480 mod 11 = 4; 2nd Chk Dgt

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


Messages In This Thread
RE: checkdigit calculation for HP-17b - Gerson W. Barbosa - 07-28-2015 06:48 PM



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