Skip to main content
📐 Mathematical Standards ISO/IEC 7812

The Luhn Algorithm Explained: Formula, Checksum & Worked Example

An in-depth technical examination of the ISO/IEC 7812 Modulo 10 formula used worldwide to detect typographical errors and validate credit card check digits.

Want to test an actual number against Luhn?

Use our interactive calculator to see real-time doubling and sum breakdowns for any number.

Open Luhn Checker Tool
Developer & QA Testing Fixtures Only

Synthetic cards are generated strictly for software engineering, user-interface validation, and automated QA tests. They are not issued by financial institutions, carry no credit balance, and are not intended or verified for live payment authorization.

Origin & Purpose of the Luhn Formula

The Luhn algorithm—also universally recognized as the Modulo 10 or Mod 10 algorithm—was developed in 1954 by German-American IBM computer scientist Hans Peter Luhn (US Patent 2,950,048, granted August 23, 1960). Originally designed for mechanical computation devices, it was subsequently standardized as an integral part of ISO/IEC 7812 for identification cards.

Today, it serves as the universal checksum mechanism for global payment card schemes (Visa, Mastercard, American Express, Discover, JCB, and UnionPay), cellular IMEI numbers, and numerous national identification systems. Its primary objective is simple: protect systems against unintentional data-entry mistakes before expensive or irreversible operations are triggered.

How the Checksum Verification Algorithm Works

Given a complete Primary Account Number (including the terminal check digit), the verification procedure follows five sequential steps:

01

Right-to-Left Traversal

Begin at the rightmost character (the check digit at position 1) and process digits from right to left. The check digit itself is in an odd position and is not multiplied.

02

Double Every Second Digit

Multiply every second digit (positions 2, 4, 6, 8, etc., counting from the right) by 2.

03

Sum Individual Digits of Products

If multiplying a digit produces a two-digit result greater than 9 (e.g., 7 × 2 = 14), add its constituent digits together (1 + 4 = 5), or equivalently subtract 9 (14 - 9 = 5).

04

Aggregate Total Sum

Add all processed numbers together—including the unaltered odd-positioned digits and the check digit itself.

05

Modulo 10 Remainder Test

Evaluate the modulo 10 remainder of the total sum: totalSum % 10 === 0. If the remainder is 0, the number satisfies the Luhn formula. If the remainder is non-zero, an error has occurred.

Step-by-Step Worked Example

Let us test the canonical ISO/IEC 7812 sample account number: 79927398713 (where 3 is the check digit):

Step Description 1 2 3 4 5 6 7 8 9 10 Check
Original Digits 7 9 9 2 7 3 9 8 7 1 3
Doubling Multiplier ×2 ×1 ×2 ×1 ×2 ×1 ×2 ×1 ×2 ×1 ×1
Raw Products 14 9 18 2 14 3 18 8 14 1 3
Summed Components 5 9 9 2 5 3 9 8 5 1 3

Addition: 5 + 9 + 9 + 2 + 5 + 3 + 9 + 8 + 5 + 1 + 3 = 70

Modulo 10: 70 % 10 = 0

✓ PASS: The total sum is exactly divisible by 10, confirming valid Luhn structure.

How to Calculate the Check Digit for a New Number

When generating a new test credit card number (such as in our Credit Card Generator), the first N-1 digits are generated first. The terminal check digit \(x\) is calculated to bring the sum up to the next multiple of 10:

1. Compute partial sum of the payload with doubling applied to positions: partialSum

2. Calculate check digit: checkDigit = (10 - (partialSum % 10)) % 10

If the partial sum modulo 10 is 0, the check digit is 0.

Reference TypeScript Implementation

Here is the high-performance TypeScript implementation used in this project's Credit Card Validator:

/**
 * Validates a numeric string against the ISO/IEC 7812 Modulo 10 Luhn formula.
 * Executes in O(N) time with O(1) auxiliary memory.
 */
export function validateLuhn(pan: string): boolean {
  const sanitized = pan.replace(/\D/g, '');
  if (sanitized.length < 2) return false;

  let sum = 0;
  let shouldDouble = false;

  // Process right-to-left
  for (let i = sanitized.length - 1; i >= 0; i--) {
    let digit = sanitized.charCodeAt(i) - 48;
    if (digit < 0 || digit > 9) return false;

    if (shouldDouble) {
      digit *= 2;
      if (digit > 9) digit -= 9;
    }

    sum += digit;
    shouldDouble = !shouldDouble;
  }

  return sum % 10 === 0;
}

Frequently Asked Questions

Why do payment processors use the Luhn algorithm instead of cryptographic hashes?

The Luhn algorithm was designed specifically for lightweight mechanical and digital error detection, not cryptographic secrecy or anti-fraud protection. Its computational simplicity allows browsers, mobile apps, and point-of-sale terminals to instantly catch accidental typing mistakes without requiring server round-trips or heavy CPU operations.

What types of input errors does the Luhn algorithm detect?

The Luhn algorithm detects single-digit errors and many common data-entry mistakes, including most adjacent digit transpositions. A known exception is 09 ↔ 90.

Does passing the Luhn checksum mean a card or account is valid?

No. A passing Luhn checksum only means the number satisfies the mathematical Modulo 10 checksum. It does not verify account existence, issuer status, available balance, CVV/CVC, expiration status, or payment authorization, and it does not contact a card issuer or payment network.

Does this site submit entered numbers to a server when calculating Luhn?

Any interactive Luhn calculation on this site runs locally in the browser and does not intentionally submit the entered test number to our application server. Use synthetic test data rather than real payment credentials.

Related Payment Testing Tools & Resources