checkdigit calculation for HP17b

07282015, 10:11 PM
Post: #21




RE: checkdigit calculation for HP17b
(07282015 06:22 PM)Thomas Klemm Wrote: This is a stepbystep 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 19. 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 

07292015, 01:52 AM
Post: #22




RE: checkdigit calculation for HP17b
(07282015 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++) \(\cdots\) could be replaced by: Code: for (i=1; i<=9; i++) 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++) \(\cdots\) could be replaced by: Code: for (i = 1; i <= 9; i++) This looks like duplicated code that could be extracted to a separate function. Cheers Thomas 

07292015, 02:20 AM
Post: #23




RE: checkdigit calculation for HP17b
(07282015 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. 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) Code: 10 * (12  i) Kind regards Thomas 

07292015, 08:30 AM
Post: #24




RE: checkdigit calculation for HP17b
(07292015 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) { At least that makes it more apparent that you run the same verification twice. The 2nd time just shifted by one digit. Cheers Thomas 

« Next Oldest  Next Newest »

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