What Is the Luhn Algorithm? The Math Behind Secure Credit Card Numbers

Find out how this simple algorithm from the 1960s keeps your wallet safe

If you’re enjoying this article, consider supporting our award-winning journalism by subscribing. By purchasing a subscription you are helping to ensure the future of impactful stories about the discoveries and ideas shaping our world today.

The first digit is the major industry identifier. Visas always begin with a 4, and Discovers always start with a 6. The next five to seven digits pinpoint the bank or institution that issued the card. What remains (sans the final digit) is your specific account number within that bank. The final digit, sometimes called the “check digit,” has nothing to do with financial institutions. Issuers tack it on so that the whole card number will satisfy a specific mathematical test—the Luhn algorithm. Here’s how the algorithm works:

Write out all but the last digit of the card number.

Double every other number starting at the right.

Sum the resulting digits (not numbers). E.g., if you doubled a 7 to become a 14 in Step 2, this will become 1 + 4 = 5 in this step.

Dutch mathematician Jacobus Verhoeff reported in 1969 that two errors—mistyping a single digit and swapping two neighboring digits of one’s card number—account for nearly 90 percent of all human input errors in practice. Verhoeff developed an even more comprehensive algorithm that, in addition to detecting all of the same typos as Luhn’s algorithm, also catches 09/90 transpositions, as well as more exotic slipups. Verhoeff’s algorithm was a mathematical triumph. Some contemporaries had even published false proofs claiming that no single check digit could carry enough information to catch all of these errors. Verhoeff’s algorithm never gained widespread adoption, however, perhaps because of its increased complexity over Luhn’s or because Luhn’s algorithm was already deployed and did a sufficient job.

Luhn’s algorithm saves you time and businesses money. At some point during a purchase, a vendor will verify that your card belongs to you by sending your information to a specialized card-validation service. This communication takes time and incurs processing fees for the business. It would be a waste of seconds and cents to outsource needless back-and-forth with a professional validation service to catch common typos. Because Luhn’s algorithm requires so little processing power, the computer handling the transaction can also run the check without needing to contact any third party.

Next time a checkout page flashes that annoying error message, remember: a simple piece of math under the hood just saved a little time and money for everyone involved.

Jack Murtagh is a freelance math writer and puzzle creator. He writes a column on mathematical curiosities for Scientific American and creates daily puzzles for the Morning Brew newsletter. He holds a Ph.D. in theoretical computer science from Harvard University. Follow Jack on X @JackPMurtagh