Questions and Answers                                                                                                     Back

Q.  How does the DCMS know that a credit card number is valid?

The LUHN Formula
 
Most credit cards contain a check digit, which is the digit at the end of the credit card number. The first part of the credit-card number identifies the type of credit card (4 = Visa, 5 = MasterCard, 3 = American Express, etc.), and the middle digits identify the bank and customer. The LUHN formula was created in the late 1960s by a group of mathematicians. Shortly thereafter, credit card companies adopted it. Because the algorithm is in the public domain, it can be used by anyone. 

The Luhn formula is also used to check Canadian Social Insurance Number (SIN) validity. In fact, the Luhn formula is widely used to generate the check digits of many different primary account numbers. Almost all institutions that create and require unique account or identification numbers use the Luhn formula. 

To generate the check digit, the Luhn formula is applied to the number. To validate the credit-card number, the check digit is figured into the formula. 

Here's how the algorithm works for verifying credit cards; the math is quite simple: 

1) Starting with the second to last digit and moving left, double the value of all the alternating digits. Where that result of the doubling is a 2 digit number (like 10, 12, 14, etc), then you add the two digits (such as 14 = 1 + 4 = 5) 
 

2) Starting from the left, take all the unaffected digits and add them to the results of all the individual digits from step 1. Again, if the results from any of the numbers from step 1 are double digits, make sure to add the two numbers first (i.e. 18 would yield 1+8). Basically, your equation will look like a regular addition problem that adds every single digit. 

3) The total from step 2 must end in zero (i.e. 50, 60, 70, 80) for the credit-card number to be valid. 

Example:  Is  4511 6699 1155 7713 a valid VISA number? Apply the Luhn formula below.

4   5   1   1      6   6   9   9       1   1   5   5       7   7   1   3

8 + 5 + 2 + 1  +  1+2 +6+ 1+8 +9   +   2 + 1+ 1+0 +5   +  1+4 +7 + 2 + 3 

The red figures in the above line show the doubled numbers, starting from the second last number and working forward. These numbers are underlined. Notice that the 9 is doubled to 18, but that is treated as 1+8 in the formula.  The total of the formula on the second line is 69. That means that the number is wrong, since the sum of the formula must end in a zero to be correct (such as 50, 60, 70, 80) 

If the number on the top line had one of the non-underlined numbers increased by 1 (i.e. 4611 instead of 4511 at the beginning, then the resultant sum would have been 70 and the number would have checked out as valid. Interestingly enough, if the second 9 in the second group would have been a zero instead, it also would have checked out as having a sum of 60, so the Luhn formula is not proof you have the right number, just a valid number. It does not give you the tools to go "guessing" at the right number.

The Luhn formula is built in to the DCMS to help you in typing in credit card numbers and will catch you if you make a typing mistake or transpose a few digits. Hope this helps explains this simple, yet complex formula. 
 

Back