Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!usc!henry.jpl.nasa.gov!elroy.jpl.nasa.gov!mahendo!wlbr!pmdvax!staylor From: staylor@pmdvax.UUCP (Scott Taylor) Newsgroups: comp.lang.c Subject: Re: The 12121 check digit algorithm Message-ID: <117@pmdvax.UUCP> Date: 7 Jan 90 20:02:37 GMT References: <1498@skye.ed.ac.uk> Lines: 36 in article <1498@skye.ed.ac.uk>, ken@aiai.ed.ac.uk (Ken Johnson) says: > * check_digit_for computes and returns the check digit for a given number, > * the number having first been stripped of any check digit. > * How it works: Assume the number has the digits abcde. Then add together > * a+c+e+2*b+2*d. To that total add 1 if b>=5 and another 1 if d>=5. The > * check digit is that total modulo 10. With all the copyright crap aside (why do you people bother arguing?) a much better method, used by the Visa and MasterCard people, and heavily in the COBOL world, is the following (which ken's seems to be a variant of): a + SUM(b) + c + SUM(d) + e Where: SUM(x) :: The sum of the digits of x added to itself, e.g. 1 = 1+1 = 2 2 = 2+2 = 4 3 = 3+3 = 6 4 = 4+4 = 8 5 = 5+5 = 10 = 1 + 0 = 1 6 = 6+6 = 12 = 1 + 2 = 3 You got the idea... Again, the check digit is MOD 10, however it will be the difference of 10 less the check digit MOD 10 ( 9 -> 10 - 9 = 1). This method is very accurate at determining transposed digits too. Try it on your Visa or MC. For 16 digit cards, perform SUM on the odd number digits; for 13 digit Visa numbers, the even digits. The last digit is the Check so don't include it. I have algorithms in C and COBOL if anyone needs them. I would have posted them here already, however I had already posted the article. -- Scott G. Taylor Pmd Resources (818) 991-0068 {wlbr,mahendo}!snidely!staylor 31230 Cedar Valley Dr. Westlake Village, CA 91362 "Vienoti Latvijai!"