Look at the long number on a bank card. The last digit isn't part of your account at all. It is computed from the other fifteen, and its only job is to notice when someone copies the number wrong. The trick behind it, the Luhn algorithm, was patented by an IBM engineer in the 1950s and still runs every time you type a card number into a checkout form.

Break it yourself

Below is a made-up 16-digit number with a correct check digit at the end (yellow). Tap ▲ or ▼ to change any digit, or switch on swap mode and tap the ⇄ tabs to swap two neighbours. Watch the checksum react.

Luhn checktap ▲ ▼ to change a digit
Blue cells get doubled (and 9 subtracted if the result is over 9). The small number under each digit is what it adds to the total. The number is valid when the total ends in 0.

The recipe

Luhn is simple enough to do on the back of an envelope:

  1. Start at the rightmost digit (the check digit) and move left.
  2. Leave that digit alone, double the next one, leave the next, double the next, and so on.
  3. If doubling gives a two-digit number, add its digits together. That is the same as subtracting 9: 7 × 2 = 14, and 1 + 4 = 5 = 14 − 9.
  4. Add everything up. If the total ends in 0, the number passes.

To create a check digit, a bank runs the same recipe on the first fifteen digits and picks whichever final digit, 0 to 9, makes the total land on a multiple of ten. There is always exactly one.

Why double every other digit?

A plain sum of the digits would already catch most single typos: change one digit and the total changes by 1 to 9, which can never be a multiple of 10. But a plain sum is blind to the second most common slip, swapping two neighbours. Addition doesn't care about order, so …38… and …83… give the same total.

Doubling alternate positions breaks that symmetry. Here is what each digit turns into when it sits in a doubled slot:

Two things jump out. First, the blue row is still every digit from 0 to 9, just shuffled. So a single wrong digit in a doubled slot also changes the total by something that isn't a multiple of 10. Every single-digit typo is caught, wherever it lands.

Second, look at how much each digit gains by being doubled (the small grey number). When you swap two neighbours a and b, one of them moves into a doubled slot and the other moves out. The total shifts by the difference between their gains. Those gains are all different from each other modulo 10, with one exception: 0 and 9 both gain nothing (0 stays 0, and 9 becomes 18, then 1 + 8 = 9). So swapping 09 for 90 slips through. Every other adjacent swap is caught. Try it with the button above.

Catching the mistakes people really make

That design matches how humans actually garble numbers. In work published in 1969, the Dutch mathematician Jacobus Verhoeff analysed errors in six-digit numbers from the Dutch postal service. About 79% were a single wrong digit and about 10% were two neighbours swapped. Luhn catches all of the first kind and almost all of the second. Rarer slips, like a doubled digit turned into another doubled digit (22 typed as 55, or 33 as 66), can sneak past.

It is worth being clear about what Luhn is not. It isn't security. Anyone can compute a valid check digit, and about one in ten completely random numbers passes by pure chance. Its purpose is to stop honest mistakes cheaply, before a form wastes a trip to the bank's servers or a clerk files a payment under the wrong account.

A patent for a hand gadget

Hans Peter Luhn filed the patent in January 1954, and it was granted in August 1960 as US 2,950,048, "Computer for Verifying Numbers". The "computer" in the title is a small mechanical hand device, not an electronic machine: you entered digits with a stylus and it produced the check digit for you.

Better schemes exist. ISBN-10 book numbers use a weighted sum modulo 11 that catches every single error and every swap, at the cost of sometimes needing an "X" as the check digit. Verhoeff designed a method in 1969 that catches all single errors and all adjacent swaps using only the digits 0 to 9. But Luhn is easy to compute by hand and easy to wire into old machines, so it stuck. Today it guards credit card numbers, the IMEI number that identifies every mobile phone, Canadian social insurance numbers and many other IDs.

Next time a form rejects your card before you've even pressed "pay", that's a 1950s checksum spotting your typo in a fraction of a millisecond.