Post Reply 
checkdigit calculation for HP-17b
07-26-2015, 11:03 PM (This post was last modified: 07-28-2015 02:32 AM by Gerson W. Barbosa.)
Post: #10
RE: checkdigit calculation for HP-17b
Don, here is a Free Pascal version:

Code:

Program CPF;
Uses Crt;
var c, 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;
    c:=10*((s Mod 11) Mod 10) + ((t Mod 11) Mod 10);
    WriteLn(c:2)
end.

Run:

999999999
99
Type EXIT to return...


Again, no attempt has been made to optimize the program, but it's interesting to compare the number of characters in both the Pascal and 17Bii codes.

Notice the algorithm I've used is somewhat different (and easier to implement on the 17Bii) than the one in the Wikipedia article. Nonetheless, I get exactly the same results.

Gerson.

Edited to fix program line #17

Edited again to fix algorithm (inclusion of an IF-clause)

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;
  if (s Mod 11)=10 then
    t:=t+9;
  d1:=(s Mod 11) Mod 10;
  d2:=(t Mod 11) Mod 10;
  GotoXY(10,1);
  WriteLn('-',d1:1,d2:1)
end.

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

Run

123456789 

123456789-09
Type EXIT to return...

As I said, this algorithm is more simple than the official one [ function TestaCPF(strCPF) ]. I have yet to discover why this works.
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-26-2015 11:03 PM



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