Double every second digit from the right, subtract 9 from anything over 9, and add it all up. A valid number totals a multiple of ten. The doubling is what makes position matter, and that is what lets Luhn catch a transposition — the error a plain digit sum cannot see.
How the doubling works
Three steps, and the second is the one doing the real work.
Count from the right. The check digit is the last one, and the doubling starts with the digit next to it. Counting from the left instead is the commonest implementation bug, because it gives the right answer only for even-length numbers.
Double every second digit. If the result exceeds 9, subtract 9 — which is the same as adding its two digits, since 16 becomes 7 either way.
Add everything. Doubled values and untouched ones alike. A multiple of ten means the number passes.
The doubling is the whole point. It makes a digit’s contribution depend on where it sits, so moving a digit changes the total — which a plain sum would not notice.
What it catches
Luhn is a typo detector, and it is a good one for its size.
Every single-digit error. Mistype any one digit and the check fails, without exception. This page asserts that on two hundred generated corruptions.
Almost every transposition. Swap two adjacent digits and it is caught — with one exception, below.
That second property is the interesting one, because it is exactly what casting out nines cannot do. A digit sum is unchanged when two digits swap, so any check built on one passes every transposition. Weighting by position is the fix, and it is why every modern check digit scheme uses one.
Transpositions matter because they are among the commonest errors people make when copying numbers — more common than single-digit substitutions in some studies. A scheme that misses them all is missing the wrong half.
What it misses
One transposition escapes: 09 and 90.
Swapping those two digits leaves the Luhn total unchanged, so the check passes on a number that is wrong. It is a known and accepted limitation rather than a bug, and the tool above will show it to you if you enter a number containing that pair.
It also misses most two-digit errors that happen to compensate for each other, and it catches about 90% of twin errors like 22 becoming 55.
Better schemes exist. The Verhoeff algorithm catches all single-digit errors and all transpositions including 09/90, using a dihedral group rather than simple doubling. It is strictly better and almost nobody uses it, because Luhn was standardised first and is easier to compute by hand.
Where it is used
Luhn is in more places than most people realise.
Payment cards. Every card number ends in a Luhn check digit, which is why a mistyped number is rejected instantly rather than after a network round trip.
IMEI numbers. Every mobile phone’s identifier carries one.
National identifiers. Canadian Social Insurance Numbers, South African ID numbers, and several others.
Test numbers. The card numbers used in payment sandboxes are Luhn-valid and belong to no account, which is exactly why they work for testing form validation.
Hans Peter Luhn patented it at IBM in 1960. The patent expired long ago, which is part of why it spread so widely.
What it does not prove
Worth being explicit, because this gets misunderstood in both directions.
A valid Luhn check means the number is well-formed. It does not mean an account exists, that it has funds, that it belongs to anyone, or that it is not stolen.
It offers no security whatsoever. The algorithm is public and takes a moment to compute, so generating valid-looking numbers is trivial. It stops typos, not fraud.
Its actual value is in the user experience and in bandwidth: catching a mistyped digit in the browser, before a request is sent, is faster and less frustrating than catching it at the payment processor.
Treating a Luhn check as validation of anything more than formatting is a genuine mistake, and one that appears in real systems.
Sources and methodology
Luhn is a published standard; these are the references.
Method. Both directions are computed by the same routine, so generating a check digit and then validating the result cannot disagree — the suite generates five hundred check digits and requires every resulting number to validate. The transposition test is run on the reader’s own number rather than described: the first adjacent unequal pair is swapped and re-checked, which also surfaces the 09/90 pair Luhn cannot catch. Every single-digit corruption is asserted to be caught across two hundred generated cases. That engine is verified on every change against 90 hand-written assertions, including that every generated check digit produces a number that validates, and that every single-digit error is caught across two hundred generated corruptions. The count and the per-case breakdown are published on the formula verification page.
Read the guide
Casting out nines is the check that cannot catch a transposition — the Digital Root Calculator shows why, and lets you watch a wrong sum pass.