What is the Luhn Algorithm?
The Luhn algorithm, also known as the mod 10 algorithm, is a simple checksum formula used to validate a variety of identification numbers, most notably credit card numbers. This algorithm is used by banks and financial institutions to detect errors.
How Does the Luhn Algorithm Work?
To apply the Luhn algorithm to a credit card number, start from the rightmost digit (the check digit). Double every second digit from the right (i.e., if the second-to-last digit is odd, double it). If doubling of a digit results in a two-digit number, add the digits together (e.g., 8 doubled is 16, and 1+6 equals 7). Sum all of the digits.
Example
Let's use the number 4388576018402634 as an example. Starting from the rightmost digit:
- 6
- 3*2 = 6
- 4
- 4
- 7*2 = 14
- 1+4 = 5
- 8
- 6*2 = 12
- 8
- 5
- 0
- 1
- 2*2 = 4
Add up all the digits: 6 + 6 + 4 + 4 + 5 + 8 + 12 + 8 + 5 + 0 + 1 + 4 = 62.
Check if the Total is Divisible by 10
If the sum of the digits is divisible by 10, then the credit card number is valid. If not, the number is invalid.