Number Theory

Check Digits, How Modular Arithmetic Catches Typing Errors

Work through the check digits on barcodes, books, cards and bank accounts, and learn which typing mistakes each scheme can and cannot catch.

Key takeaways

  • A check digit scheme requires a weighted sum of all the characters to satisfy a congruence.
  • Every single-digit error is caught when each weighting step can never give two different digits the same contribution.
  • Swapping neighboring digits is harder to detect, and several common base-10 schemes miss some of those swaps.
  • Working modulo a prime, as in the ten-digit ISBN with 11, catches all single errors and all transpositions.
  • Check digits guard against accidents and offer no protection against deliberate forgery.

A check digit is an extra symbol computed from the rest of a number, so that a mistyped or misread number usually fails a simple test. The test is almost always a statement about remainders: some weighted sum of the digits must leave a particular remainder when divided by 10, 11 or another fixed number.

You meet these on product barcodes, book numbers, payment cards, bank account numbers and many identity documents. Once you see the arithmetic, you can also predict which errors will slip through.

The basic idea

Suppose you add all the digits of an account number and append whatever digit makes the total end in 0. For 5273, the sum is 17, so you append 3 and issue 52733.

Mistype one digit and the sum changes by an amount between −9 and 9, but not 0. The total no longer ends in 0, so the error is caught. Swap two digits, as in 25733, and the sum is unchanged. The mistake sails through.

That weakness motivates weights: multiply each digit by a number that depends on its position, so that order matters. The language for all of this is modular arithmetic, where you keep only the remainder.

What kinds of mistakes happen

When people copy numbers, the usual slips are easy to list: one wrong digit, two neighbors swapped, a digit dropped or doubled, or a whole block misplaced. A dropped or extra digit is caught by the fixed length of the number. So the design effort goes mainly into the first two.

A scheme can be judged by two conditions on its weights and modulus. To catch every single error, changing a digit must always change the total modulo the base number. A weight of 2 with modulus 10 fails, because the digits 0 and 5 contribute 0 and 10, which look identical. To catch a swap of two positions, the difference of their weights, multiplied by the difference of the digits, must not be divisible by the modulus.

Both conditions are questions about shared factors. That is why the choice of modulus matters as much as the choice of weights.

Product barcodes

The common 13-digit product number uses weights 1, 3, 1, 3 and so on from the left. The weighted total of all 13 digits must be divisible by 10. The 13-digit form of the ISBN for books follows the same rule.

Take the first twelve digits 978030640615.

digits in odd positions:   9, 8, 3, 6, 0, 1    sum 27, weight 1 → 27
digits in even positions:  7, 0, 0, 4, 6, 5    sum 22, weight 3 → 66
total 93
check digit = 7, since 93 + 7 = 100

The 12-digit code long used in North America is the same pattern with the weights starting at 3. For 03600029145, the odd positions sum to 14, which contributes 42, and the even positions contribute 16. The total is 58, so the check digit is 2.

Changing one digit by d changes the total by d or 3d. Since 3 shares no factor with 10, neither is a multiple of 10, so every single-digit error is detected. How the bars themselves are decoded is a separate story, told in how barcodes and QR codes are read.

The blind spot of weights 1 and 3

Swap neighboring digits a and b. One had weight 1 and the other weight 3, and they trade places. The total changes by (3b + a) − (3a + b) = 2(b − a).

That is divisible by 10 whenever a and b differ by exactly 5. So a swap of 1 and 6, or of 2 and 7, goes unnoticed. Every other swap of unequal neighbors is caught.

This is a design compromise. A single base-10 check digit with simple weights cannot catch both all single errors and all neighboring swaps.

The ten-digit ISBN and the power of a prime

Older book numbers have ten characters weighted 10, 9, 8 and so on down to 1. The weighted total must be divisible by 11. If the required check value is 10, it is written X.

For the nine digits 030640615:

0·10 + 3·9 + 0·8 + 6·7 + 4·6 + 0·5 + 6·4 + 1·3 + 5·2 = 130
130 leaves remainder 9 on division by 11
check digit = 2, since 130 + 2 = 132 = 12 · 11

Because 11 is prime, a product of two numbers is divisible by 11 only if one of them is. A single error d in a position of weight w changes the total by wd, and neither factor is a multiple of 11. Swapping the digits in positions of weights v and w changes the total by (v − w)(b − a), again not a multiple of 11 when the digits differ. All single errors and all swaps, adjacent or not, are caught.

The price is the extra symbol X. When book numbering moved to thirteen digits to join the product code system, it gave up some of this strength.

Payment cards and the Luhn method

Most payment card numbers satisfy the Luhn formula, named for the computer scientist Hans Peter Luhn.

  1. Starting from the rightmost digit, which is the check digit, move left.
  2. Double every second digit, beginning with the one next to the check digit.
  3. If a doubled value exceeds 9, subtract 9.
  4. Add everything. The total must be divisible by 10.

Try the test string 79927398713, reading from the right. The digits left as they are, 3, 7, 9, 7, 9 and 7, sum to 42. The others are 1, 8, 3, 2 and 9. Doubling them and subtracting 9 where needed gives 2, 7, 6, 4 and 9, which sum to 28. The total is 42 + 28 = 70, so the string passes.

The subtraction step shuffles the ten digits without collisions, so single errors are always caught. Among neighboring swaps, only 09 and 90 are missed.

Bank account numbers and modulus 97

The International Bank Account Number carries two check digits and works modulo 97, the largest two-digit prime.

  1. Move the first four characters to the end.
  2. Replace each letter by a number: A is 10, B is 11, up to Z, which is 35.
  3. Read the result as one long integer and divide by 97.
  4. A valid number leaves remainder 1.

The widely published sample GB82 WEST 1234 5698 7654 32 passes this test. You can compute the remainder in chunks, carrying each partial remainder into the next group of digits, so no huge arithmetic is needed. Two check digits and a prime modulus catch all single errors, nearly all swaps, and about 99 percent of random garbling.

Choosing a scheme

What each scheme is built to catch
Weights 1 and 3, mod 10 All single-digit errors Misses neighbor swaps that differ by 5 Check symbol is always a digit Luhn, mod 10 All single-digit errors Misses only the 09 and 90 neighbor swap Check symbol is always a digit ISBN-10, mod 11 All single-digit errors All swaps of unequal digits Sometimes needs the symbol X IBAN, mod 97 All single-digit errors Nearly all swaps Uses two check digits Weights 1 and 3, mod 10 All single-digit errors Misses neighbor swaps that differ by 5 Check symbol is always a digit Luhn, mod 10 All single-digit errors Misses only the 09 and 90 neighbor swap Check symbol is always a digit ISBN-10, mod 11 All single-digit errors All swaps of unequal digits Sometimes needs the symbol X IBAN, mod 97 All single-digit errors Nearly all swaps Uses two check digits

Can one decimal check digit catch every single error and every neighboring swap, with no extra symbol? Yes, if you leave ordinary arithmetic modulo 10 behind. J. Verhoeff's method of 1969 uses the symmetries of a regular pentagon, a group of ten elements in which order of multiplication matters. H. Michael Damm later gave another solution based on a specially built operation table. The idea of a group is introduced in a first look at group theory.

An older cousin: casting out nines

Before electronic calculators, clerks checked arithmetic using remainders modulo 9. A number leaves the same remainder on division by 9 as the sum of its digits. To test the claim 347 × 26 = 9022, reduce each part.

347 → 3 + 4 + 7 = 14 → 5
26  → 2 + 6 = 8
5 × 8 = 40 → 4
9022 → 9 + 0 + 2 + 2 = 13 → 4

The two sides agree, as they must if the product is right. Agreement does not prove correctness: the wrong answer 9202 has the same digits and also gives 4. The method is blind to swapped digits for the same reason that an unweighted check digit is. It rests on the same facts as the familiar divisibility rules.

How much random garbage gets through

If a string were completely random, it would pass a test modulo 10 about one time in 10, modulo 11 about one time in 11, and modulo 97 about one time in 97. Real errors are far from random, which is why the targeted guarantees for single digits and swaps matter more. Systems that need stronger protection add more check characters or use longer algebraic codes.

Detection, correction and security

A check digit tells you that something is wrong, not what. Many different single changes would repair the total. To locate and fix errors you need more redundancy, arranged cleverly, which is the subject of error-correcting codes.

Nor is a check digit a security measure. The rules are public, so anyone can produce a string that passes. A card number that satisfies the Luhn test need not belong to any account. Detecting deliberate tampering calls for cryptographic tools such as secure hash functions combined with keys or signatures.

Try it on something nearby

Pick up any packaged product and find its barcode number. Add the digits with alternating weights, making sure that the final check digit gets weight 1. The total should end in 0.

Then experiment. Change one digit and confirm that the test fails. Look for two neighbors that differ by 5, swap them, and watch the test pass anyway. Finding that failure yourself is the quickest way to understand why the choice of weights and modulus matters.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 59 articles in Number Theory →

Latest news on this topic