Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What algorithm to use to calculate a check digit?

What algorithm to use to calculate a check digit for a list of digits?
The length of the list is between 8 and 12 digits.

see also:
How to generate a verification code/number?

like image 816
Piotr Dobrogost Avatar asked Jul 08 '09 21:07

Piotr Dobrogost


People also ask

Which algorithm is used to calculate check digit of a card number?

The Luhn formula applies some basic arithmetic to an identification number to calculate a value that must agree with the check digit -- which, for example, is the last number that appears on the credit card.

What is digit algorithm?

An algorithm which allows digits of a given number to be calculated without requiring the computation of earlier digits. The BBP formula for pi is the best-known such algorithm, but an algorithm also exists for e.

How do you find the check digit of an identification number?

To calculate the check digit, add up the first 10 digits and the sum is divided by 9. The remainder is the check digit.


3 Answers

The Luhn algorithm is good enough for the credit card industry...

like image 111
RichieHindle Avatar answered Oct 10 '22 00:10

RichieHindle


As RichieHindle points out, the Luhn algorithm is pretty good. It will detect (but not correct) any one error or transposition (except a transposition of 0 and 9).

You could also consider the algorithm for ISBN check digits, although for old-style ISBN, the check digit is sometimes "X", which may be a problem for you if you're using integer fields. New-style ISBN doesn't seem to have that problem. Wikipedia doesn't go in to the theoretical properties of the system, but I remember studying ISBN numbers in my coding theory course long ago, so I think they are pretty good :-)

like image 33
John Fouhy Avatar answered Oct 09 '22 22:10

John Fouhy


I know it is a bit late (according to post dates), but first time I needed a check number algorithm was last week.

So I checked more algorithms and IMHO the best solution seems to be the Damm algorithm. It is simple to implementation and detect most of tested errors. With default digit check table all single digit errors, all English language mishearing errors, all adjacent transposition errors, and almost all jump transpositions errors are detectable.

For me there was only a single problem, since I need to calculate check digit not only from numbers but also from characters. Unfortunately for me, there was a given rule, that the last character must be a digit; or better to say, the characters were assigned by third party authority and only fixed amount of numbers were used as manufacturer number.

There are many ways how to transcribe characters to number, but the error detection will always be lower, comparing to when only numbers are used.

For these cases you can use the ISO_6346 specification.

When there is no such limitation, use the tables for different size and assign characters and number to table values.

EDIT: updated/fixed description, added reason for digit check number for characters, and added tables for different base sizes.

like image 4
Julo Avatar answered Oct 09 '22 23:10

Julo