Post Reply 
checkdigit calculation for HP-17b
07-29-2015, 01:52 AM
Post: #22
RE: checkdigit calculation for HP-17b
(07-28-2015 09:51 PM)Gerson W. Barbosa Wrote:  I wonder why they use a somewhat more complicate method here, that is, multiplications of the sums by 10 being required before the final mod 11 operation

These lines:
Code:
for (i=1; i<=9; i++)
    Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (11 - i);
Resto = (Soma * 10) % 11;

\(\cdots\) could be replaced by:
Code:
for (i=1; i<=9; i++)
    Soma += parseInt(strCPF.substring(i-1, i)) * i;
Resto = Soma % 11;

Quote:not to mention the appending of the first checking digit to the number before computing the second checking digit.

These lines:
Code:
for (i = 1; i <= 10; i++)
    Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (12 - i);
Resto = (Soma * 10) % 11;

\(\cdots\) could be replaced by:
Code:
for (i = 1; i <= 9; i++)
    Soma += parseInt(strCPF.substring(i, i+1)) * i;
Resto = Soma % 11;

This looks like duplicated code that could be extracted to a separate function.

Cheers
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-29-2015 01:52 AM



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