The Luhn algorithm returns a pass when the weighted digit sum ends in 0. Test case 79927398713 passes. Test case 79927398714 fails. Both strings share the same first ten digits, so the last digit carries the whole result.

luhn algorithm check test

How the check digit is built

Start at the rightmost digit and move left. Double the value of every second digit. If a doubled value reaches 10 or more, subtract 9 or add the two digits of the result. Add every term. If the total mod 10 equals 0, the string passes.

related article

A two-digit example: the string 18. The rightmost digit 8 stays at 8. The digit 1 doubles to 2. The sum is 10, and 10 mod 10 is 0, so 18 passes. The string 19 gives 9 plus 2, which is 11. 11 mod 10 is 1, so 19 fails.

test luhn algorithm

Worked test cases

These vectors appear in test suites for Luhn libraries.

Luhn Algorithm Check Test: A Comprehensive Guide for Online CVV Verification

  • 79927398713: pass. Check digit 3.
  • 79927398714: fail. Same body, wrong check digit.
  • 79927398710: fail.
  • 1234567812345670: pass. Weighted sum 60.
  • 18: pass. Weighted sum 10.
  • 19: fail. Weighted sum 11.

Published sandbox numbers

Payment processors publish card numbers for test environments. Each one passes the Luhn check and maps to no live account.

  • 4242424242424242: 16 digits, pass.
  • 378282246310005: 15 digits, pass.
  • 6011111111111117: 16 digits, pass.

A Luhn pass says nothing about account state. It does not confirm the issuer, the balance, or that an account is open.

Errors the checksum misses

Luhn catches every single-digit typo. It also catches most swaps of two adjacent digits. It does not catch a swap of 0 and 9, because 09 and 90 produce the same weighted sum.

The check does not catch two errors that cancel out. It does not catch a string that was never issued. One random string in ten passes.

Test checklist for an implementation

  1. Strip spaces and hyphens before the check.
  2. Reject non-digit characters.
  3. Track index parity from the right.
  4. Double digits at even positions counted from the right, starting with the second digit.
  5. Sum the digits of any double that reaches 10 or more.
  6. Return pass when the total mod 10 is 0.

Origin and standard

Hans Peter Luhn filed US patent 2,950,048 in 1954. The patent issued in 1960. ISO/IEC 7812-1 lists the check digit formula for identification cards.

Luhn is a checksum, not a security control. Card systems use it to catch typing errors at the point of entry. Authorization, expiration checks, and address checks run elsewhere. A passing test vector grants no permission to use an account.