Skip to main content
Back to projects

Luhn Checksum

Algorithms

Fundamentals Interactive
Python algorithms
Interactive demo the Luhn checksum, live

Card numbers are checked with the Luhn algorithm: from the right, double every second digit, subtract 9 when a doubled digit exceeds 9, and sum everything. A valid number makes the total a multiple of 10. Type a number and watch each digit contribute.

Find a missing check digit

Enter the first 15 digits and it will compute the final digit that makes the number valid.

How it works

  1. 1 Take the number
  2. 2 Double every second digit
  3. 3 Sum the digits
  4. 4 Check the total is a multiple of 10

How it works

The Luhn algorithm catches transcription errors. From the right, every second digit is doubled; any doubled value over 9 has 9 subtracted. If the total is a multiple of 10, the number is structurally valid.

Building it, step by step

1. Process from the right

Starting at the last digit, alternate doubling every second digit as you move left.

2. Reduce doubled digits

Doubled digits of 5–9 become two-digit numbers, so 9 is subtracted to keep a single digit.

3. Sum and verify

The final digit is chosen so the whole sum is divisible by 10 — which is how a missing check digit can be reconstructed.

What I learned

A checksum is a cheap, deterministic guard that catches most single-digit and adjacent-transposition errors before any expensive lookup happens. It validates structure, not whether an account exists.