The Luhn algorithm test is a checksum calculation that tells you whether a string of digits is internally consistent, so a single mistyped or swapped digit is likely to be caught. It does not confirm that a card, account, or device number exists. It only confirms that the number's check digit matches the rest of the digits.

What the Luhn test checks

The formula is also called the mod 10 algorithm, named after Hans Peter Luhn, who developed it at IBM in the 1950s. It reads a number from right to left, applies a simple doubling rule to every second digit, and adds the results. If the sum is a multiple of 10, the number passes. If the sum ends in any other digit, the number fails.

Because the rule touches every digit, it catches most one-digit typing errors and most adjacent transpositions such as 45 typed as 54. A Luhn algorithm test cannot catch a two-digit error like 22 typed as 55, and it says nothing about the number's meaning.

How to run a Luhn check step by step

  1. Remove spaces, hyphens, and other separators so you have digits only.
  2. Start at the rightmost digit, which is the check digit, and move left.
  3. Double the value of every second digit. The check digit itself is never doubled in the standard form.
  4. If a doubled value is greater than 9, subtract 9 from it.
  5. Add the check digit and all the adjusted values together.
  6. If the total is divisible by 10, the number passes. Otherwise it fails.

Worked example

Take the number 79927398713. Reading right to left, the digits in doubled positions become 1 to 2, 7 to 14 then 5, 9 to 18 then 9, 7 to 14 then 5, 9 to 18 then 9, and 7 to 14 then 5. Adding those to the untouched digits gives 3 + 2 + 5 + 8 + 9 + 3 + 5 + 2 + 9 + 9 + 5, which equals 60. Since 60 is divisible by 10, the number passes the Luhn algorithm test.

To find the correct check digit for a partial number, run the same steps on the digits you have, then choose the final digit that makes the total end in zero.

What a passing result does not prove

  • It does not show the account exists, is open, or has funds.
  • It does not show the card is unexpired or unblocked.
  • It does not verify a name, address, or security code.
  • It is not authorization to use a number you do not own or have permission to use.

A number that passes is only a number that is formatted correctly, nothing more.

Where the Luhn formula is used

  • Payment card numbers, under the ISO/IEC 7812 numbering standard.
  • IMEI numbers on mobile devices.
  • National Provider Identifier numbers in US healthcare.
  • Canadian Social Insurance Numbers and several national ID schemes.
  • Some loyalty, transit, and insurance policy numbers.

Common mistakes when writing a Luhn test

  • Counting positions from the left instead of the right, which flips the pattern and fails valid numbers about half the time.
  • Doubling the check digit, which shifts every other digit.
  • Forgetting the subtract 9 step, which inflates the total.
  • Leaving spaces or dashes in the input before the loop runs.
  • Treating a pass as proof that data is real rather than proof that it is well formed.

Used as intended, a Luhn algorithm test is a fast input filter. It catches typos at the point of entry, before a number travels any further.