DOC

How calculators work

Modular Arithmetic: The Mathematics Behind Clocks and Check Digits

Modular arithmetic turns endless integers into repeating residue classes. Learn congruence, clock calculations, negative remainders, and how ISBN, GTIN, and Luhn check digits use the same idea.

A clock does something ordinary arithmetic never does: it lets 15 become 3 without claiming that 15 equals 3. The dial is keeping only the position left after complete turns. Modular arithmetic formalizes that wraparound. It groups infinitely many integers into a finite set of positions and asks which position an integer occupies, not how large the integer is.

That small change of viewpoint reaches far beyond clocks. It can track weekdays, rotate array positions, shorten large powers, and build check digits for identifiers. The final digit on an old ISBN-10, a GTIN-12 carried by a UPC-A barcode, or a number checked by the Luhn method is an arithmetic promise: the complete weighted sum must land on one approved residue.

A mathematician turns a giant clock while looping ribbons of geometric tokens return to shared positions
Modular arithmetic identifies positions separated by whole cycles. The integers keep going; the set of possible positions repeats.

Congruence is a relationship, not a smaller equality

For integers a and b and a positive modulus m, the central statement is:

a ≡ b (mod m)

if and only if m divides a − b.

For example, 29 ≡ 5 (mod 12) because 29 − 5 = 24, exactly two groups of 12. The statement does not erase the difference between 29 and 5. It says they belong to the same congruence class when differences of whole 12s are ignored. The MIT modular-arithmetic notes use this divisibility definition; the Cornell CS 2800 notes develop arithmetic on the resulting classes.

Division with remainder chooses one convenient representative. For positive m, every integer a can be written uniquely as:

decomposea = qm + r
canonical range0 ≤ r < m
residuer = a mod m

Here q counts complete groups of m, while r is the least nonnegative residue. With a = 29 and m = 12, q = 2 and r = 5. With a = −7, the same canonical residue is 5 because −7 = (−1)(12) + 5. Thus 29, 5, and −7 occupy the same class modulo 12.

Why a 12-hour clock displays zero as 12

Suppose it is 10 o’clock and 29 hours pass. Ordinary addition gives 39. A 12-hour dial removes three complete turns:

add elapsed hours10 + 29 = 39
remove full turns39 = 3(12) + 3
dial position39 ≡ 3 (mod 12)

The dial therefore reads 3. If the residue were 0, a conventional 12-hour clock would display 12. OpenStax’s clock-arithmetic section makes that display convention explicit. It is only a relabelling: the mathematical residue set remains {0, 1, …, 11}, not {1, 2, …, 12}.

The modulus must match the cycle being modelled. Minutes within an hour use 60; weekdays use 7; a 24-hour time display uses 24. Choosing 12 simply because the picture looks like a clock would be a modelling error if the actual system repeats on a different period.

Reduce before or after the operation

Congruence survives addition, subtraction, and multiplication. If a ≡ b (mod m) and c ≡ d (mod m), then the sums, differences, and products are congruent too. That means a calculator can replace large inputs with smaller residues, perform the operation, and reduce again.

An engineer merges two streams of tokens onto a circular track where the combined group wraps around
For addition and multiplication, reducing the inputs and then reducing the result gives the same residue as reducing only at the end.
Addition modulo 1238 + 47 ≡ 2 + 11 ≡ 13 ≡ 1

The direct total, 85, also leaves residue 1.

Subtraction modulo 122 − 5 = −3 ≡ 9

Moving three positions backward from 2 lands at 9.

Multiplication modulo 717 × 23 ≡ 3 × 2 ≡ 6

The full product, 391, leaves the same residue.

Ordinary division is the trap. Modular cancellation is safe only under the right conditions. Modulo 6, 2·1 ≡ 2·4 because both products have residue 2. Cancelling the 2 would incorrectly claim 1 ≡ 4 (mod 6). The missing fact is that 2 has no multiplicative inverse modulo 6: it shares a factor with the modulus. A factor can be cancelled without changing the modulus when that factor is coprime to the modulus.

A % key is not a universal mathematical definition

Programmers often write %, but languages are allowed to choose different remainders for negative inputs. Both of these results satisfy −5 = q·3 + r; they differ because the languages choose different quotients:

expressionresultrule behind it
Python: −5 % 31floor division; with positive divisor, the remainder is nonnegative
Java: −5 % 3−2integer division truncates toward zero; remainder follows the dividend's sign

These are specified behaviors in the Python language reference and Java Language Specification §15.17.3. If an algorithm needs a value in 0 through m − 1, normalize explicitly instead of assuming every % operator already returns that representative.

A check digit asks for one approved residue

A check-digit system attaches a final symbol so a weighted sum satisfies a modular rule. When someone mistypes a digit, the weighted sum often moves to a forbidden residue and the identifier can be rejected before a database lookup. The mechanism is small because it retains only a residue, not the entire history of the digits.

A worker feeds geometric digit tiles through a mechanical checksum gate where one misplaced tile raises a warning flag
A check digit is a compact consistency gate. An error may disturb the residue and raise a warning, but a passing result is not proof of identity or authenticity.

ISBN-10: a legacy modulus-11 rule

ISBN-10 applies weights 10 down to 1 and requires the total to be divisible by 11:

10d₁ + 9d₂ + 8d₃ + … + 2d₉ + d₁₀ ≡ 0 (mod 11)

The final symbol may be X, representing the value 10.

For the familiar example 0-306-40615-2, the weighted sum is 132, which is 0 mod 11. It passes the arithmetic test. The U.S. ISBN Agency documents the weights and X convention. This format is historical: the International ISBN Agency states that new assignments changed to 13 digits on 1 January 2007. A valid ISBN-10 checksum does not prove that a book-number pairing is genuine; that requires the authoritative registry.

GTIN-12: alternating weights modulo 10

A UPC-A barcode carries a 12-digit GTIN. For the first 11 digits, multiply alternating positions from the left by 3 and 1, then choose a check digit c that makes the total divisible by 10:

c = (10 − (S mod 10)) mod 10

For GS1's example base 61414121022, the weighted sum is 60, so c = 0 and the complete GTIN-12 is 614141210220.

This is the manual method published by GS1. The second modulo in the formula matters: when S is already divisible by 10, the check digit is 0 rather than 10.

Luhn: alternate, double, fold, and sum

For a complete number checked by the modern right-to-left Luhn procedure, keep the rightmost check digit unchanged. Moving left, double every second digit; when a doubled value exceeds 9, subtract 9. The transformed values plus the untouched values must sum to a multiple of 10.

complete number4 8 7 2 1 4 8 4
after alternating transform8 8 5 2 2 4 7 4
sum40 ≡ 0 (mod 10)

The transformation comes from Hans Peter Luhn’s original patent. The PCI Security Standards Council gives the present validation procedure—and an essential warning: passing Luhn indicates only a possible, structurally valid number, not that a payment card was issued or activated.

Detection is not correction, identity, or security

Several different digit strings inevitably share the same residue. A check digit can therefore detect useful classes of accidental entry errors without detecting every possible multi-digit change. It normally cannot locate the wrong digit or reconstruct what the user intended. Public rules also let anyone recompute a matching digit after deliberately altering the data.

The ISO/IEC 7064 overview separates error detection from automatic correction and excludes deliberate falsification from its scope. Treat the layers separately:

Arithmetic consistency

The weighted sum lands on the required residue. This is what the check digit establishes.

Registry validity

An issuer or registry confirms that the identifier was actually assigned to the claimed object.

Authenticity and security

Authentication, signatures, or cryptographic controls address deliberate manipulation. A public checksum does not.

Modular arithmetic is powerful precisely because it forgets most of a number and retains the part a cyclic problem needs. A clock needs a position, not an elapsed-hour total. A check digit needs a residue test, not a database record. The method is reliable when the modulus, residue convention, weights, and claim are all stated—and misleading when a small consistency check is asked to prove more than its mathematics contains.