The algorithm
How a number can check itself
In 1954 an IBM engineer named Hans Peter Luhn filed a patent for a tiny checksum for catching typing mistakes. Nearly every card number issued today still ends in a digit calculated this way. It takes six small steps and no lookups, so it runs anywhere.
Step 1 of 6
Start with the number
Take the digits exactly as printed. The very last digit is the check digit. It was chosen by the issuer so that the whole number works out, and everything else in the number is what it gets checked against.
- 44
- 00
- 11
- 22
- 88
- 88
- 88
- 88
- 88
- 88
- 88
- 88
- 11
- 88
- 88
- 11
What it catches
Every number with a single wrong digit, and nearly every swap of two neighbouring digits. The one pair it misses is 09 and 90. Those two give the same total either way. That covers most real typos.
What it does not
Two wrong digits can cancel each other out, and a number you invent can pass by luck. About one random number in ten does. It was designed against accidents, not against people.
Why it is not security
The recipe is public and the check digit is easy to calculate. Passing Luhn says nothing about whether a card is real or who owns it. Treat it as a spell checker for numbers.