← CS Unplugged

CS Unplugged activity

Check Digits: catching typos with modulo

About 20 minutesSoloPencil

Barcodes, ID numbers, and account numbers often carry one extra digit at the end whose only job is to catch typos. It’s called a check digit, and it’s computed with modulo. Below is a simplified version of the rule real barcodes use.

The rule: starting from the left, multiply each digit by 3, then 1, then 3, then 1, and so on, alternating. Add up all the products. The check digit is whatever number, added to that sum, makes a multiple of 10.

Worked example: the code 41729.

Digit 4 1 7 2 9
Weight ×3 ×1 ×3 ×1 ×3
Product 12 1 21 2 27

12 + 1 + 21 + 2 + 27 = 63. The next multiple of 10 is 70, and 70 − 63 = 7. So the check digit is 7, and the full code is printed as 417297.

Here’s why that catches mistakes: if you mistype any one digit, the sum almost never lands on a multiple of 10 anymore. A computer can check that in an instant, without knowing anything about what the code actually means.

Part 1: Is this code real?

Each code below ends in its check digit. Work out whether that check digit is correct.

CodeReal or fake?
417297
930822
155260
803311
225601
614479

Part 2: Find the missing digit

This time, the last digit is missing. Work out what it has to be.

CodeMissing digit
37054?
91268?
50443?

Part 1. 417297: real. 930822: fake. 155260: fake. 803311: real. 225601: real. 614479: fake.

Part 2. 37054?: 7. 91268?: 6. 50443?: 0.

Challenge. Swapping two neighboring digits changes the sum by 2 times their difference (because one was weighted ×3 and the other ×1, or vice versa). That only slips past the check digit undetected when the two digits differ by exactly 5 (0 and 5, 1 and 6, and so on). Try it both ways and you’ll see the pattern. Real barcodes use this same rule, and share this same rare blind spot.

CC BY-NC-SA 4.0.