Post Reply 
checkdigit calculation for HP-17b
07-28-2015, 10:11 PM
Post: #21
RE: checkdigit calculation for HP-17b
(07-28-2015 06:22 PM)Thomas Klemm Wrote:  This is a step-by-step transformation of your formula to my solution.

Thanks for that explanation, Thomas, it is very good.

The only reason I had that first MOD in my equation (right after CD=) is to take care of the case where the mod of the sum was 0, because when I subtracted that from 10 the result would be checkdigit 10, which is wrong of course. So, that first MOD seemed to be the easiest way to take care of the 0 and not bother the other results 1-9. I could have used an IF I guess, but I wanted to avoid IF's.

What you did would have never occurred to me because I would have wanted to avoid negative numbers, I never considered that using negative numbers could have resulted in an even better example of code, given how MOD uses INT and that treats negative numbers in an advantageous way in this particular application.

I much appreciate your insights.

Don
Find all posts by this user
Quote this message in a reply
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
07-29-2015, 02:20 AM
Post: #23
RE: checkdigit calculation for HP-17b
(07-28-2015 10:11 PM)Don Shepherd Wrote:  What you did would have never occurred to me because I would have wanted to avoid negative numbers

And that's why you used -9 in your formula?
Nah, just kidding. Of course I understand what you mean. Smile

Once you're familiar with the rules of modular arithmetic, you can juggle with the expressions in new ways.

Examples modulo 11:
(stolen from my previous answer to Gerson's question)

Code:
10 * (11 - i)
-1 * (0 - i)
i

Code:
10 * (12 - i)
-1 * (1 - i)
i - 1

Kind regards
Thomas
Find all posts by this user
Quote this message in a reply
07-29-2015, 08:30 AM
Post: #24
RE: checkdigit calculation for HP-17b
(07-29-2015 01:52 AM)Thomas Klemm Wrote:  This looks like duplicated code that could be extracted to a separate function.

Maybe something like this?
Code:
function checkCPF(cpf) {
    if (cpf == "00000000000") {
        return false;
    }
    return verify(cpf) && verify(cpf.substring(1));
}

function verify(value) {
    var CHECK = 9;
    var digit = value.split("");
    var sum = 0;
    for (var i = 0; i < CHECK; i++) {
        sum += digit[i] * (i + 1);
    }
    return (sum % 11) % 10 == digit[CHECK];
}

At least that makes it more apparent that you run the same verification twice.
The 2nd time just shifted by one digit.

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




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