Modular arithmetic performs addition and multiplication on integers while remembering only their remainders for a fixed positive integer modulus . The relation means that divides : the two integers lie in the same congruence class. For example, , and represent one class modulo . Choosing the least nonnegative residue gives exactly representatives, , even though each congruence class contains infinitely many integers.
A clock supplies a familiar example. Represent the twelve positions by , with at the position usually labelled twelve. Starting at ten and moving seven positions givesThe integer congruence records the final position; it discards the number of complete revolutions. Adding another twelve changes the elapsed count while preserving the position. The picture also shows a single congruence class modulo five spread along the number line.
The useful surprise is that discarding this information still preserves the arithmetic. If and modulo , then and modulo . Thus a large calculation can reduce its intermediate integers after every step. For instance,Replacing by gives the same answers with smaller intermediate values. The congruence classes with these operations form the quotient ring ; their addition alone forms a cyclic group. This passage from integers to a finite arithmetic system is the setting for modular exponentiation, primality testing and the RSA cryptosystem. [ref]
https://ocw.mit.edu/courses/18-200-principles-of-discrete-applied-mathematics-spring-2024/mit18_200_s24_lec15.pdf. Ankur Moitra, MIT 18.200, Lecture 15, modular arithmetic and its applications
Multiplicative cancellation needs extra care. Modulo six, , but : multiplication by two has merged distinct congruence classes. Cancelling a factor while retaining the integer modulus is justified when that factor has a modular multiplicative inverse, equivalently when it is coprime to the integer modulus. A linear congruence can consequently have no solutions, one solution class, or several solution classes. The Chinese remainder theorem supplies a complementary operation: splitting a calculation across coprime integer moduli and reconstructing its class modulo their product.
Small remainders can also certify impossibility. A perfect square has residue zero or one modulo four, as checking the four congruence classes shows. Therefore an integer equation has no solution. The converse direction needs care: a permitted residue does not guarantee an integer solution, since but five is not a perfect square. Such local tests provide necessary conditions for Diophantine equations without replacing the full problem.
In Disquisitiones arithmeticae (1801), Carl Friedrich Gauss defines integer congruence in article 1 and adopts the symbol in article 2. Article 12 derives divisibility tests from powers of ten and explains checking arithmetic by its remainders. These passages show a systematic treatment of the same idea; they are not a claim that remainders were first used in 1801. [ref]
https://la.wikisource.org/wiki/Disquisitiones_arithmeticae/Sectio_prima. Gauss, Disquisitiones arithmeticae, section I, articles 1–2 and 12; text from the 1870 collected edition
A free-running digital counter with modulus shows only the residue of elapsed ticks. If two observations differ by a true interval , their modular difference is congruent to and is the unique representative in , proving the displayed identity. It handles a wrap between observations without a conditional branch. A complete elapsed modulus is indistinguishable from zero, so the interval bound is essential. For a -bit timer at frequency , the wrap period is , and unsigned integer arithmetic in C can implement the difference directly on an appropriate type.
For an integer and a positive integer modulus , the modulo operation returns the unique least nonnegative residue satisfying for an integer and . Equivalently, . For example, . Some programming languages instead use a remainder with the sign of the dividend, so their negative-input convention must be checked before identifying an operator with this mathematical definition.
Computing the remainder while working with bounded integer representatives. Repeated doubling and addition can avoid forming an overflowing product: process the binary digits of , double the current contribution modulo , and add it to the result whenever the current binary digit is 1. The additions themselves must also use an overflow-safe implementation.
For an integer , add residue classes by adding their representatives and taking the class modulo . This is well-defined because replacing either representative by a multiple-of- translate changes the sum by a multiple of . The associativity, identity class and inverse are inherited from integer addition. The result is the additive cyclic group .
The positive integer in is the modulus. It specifies which differences are ignored: exactly the integer multiples of . It is distinct from the modulus of a complex number, which measures magnitude.
The least nonnegative residue of an integer modulo a positive integer modulus is the unique representative of its congruence class in . Euclidean division gives . For example, , so ; the integer congruence does not assert equality of those two integers.
For a positive modulus , integers are congruent when their difference is divisible by . This is an equivalence relation: zero is divisible by , changing sign preserves divisibility, and adding two divisible differences proves transitivity. Addition and multiplication respect it, because and . Its equivalence classes are the residue classes used in modular arithmetic.
For an integer , Wilson's theorem characterizes prime numbers by a factorial:The right side is an integer congruence, so the least nonnegative remainder is . For example, certifies that five is a prime number, whereas rules out six. [ref]
For an odd prime number , each nonzero congruence class has a unique modular multiplicative inverse. Pair each class with its inverse: each distinct pair contributes one. A class is its own inverse exactly when ; since , Euclid's lemma leaves only . Their product is . The prime number two needs a separate check: . Conversely, a proper divisor divides both and , so the displayed integer congruence would force , a contradiction. [ref]
The composite numbers have an even sharper pattern: for every composite number , but . To check the zero claim, write with . If , both occur as different factors in the factorial. If , the different factors and both occur since , and their product is divisible by . The only excluded case is . Thus four is an exception to the zero pattern, but still fails Wilson's theorem: its required remainder would be three.
This is an exact primality testing criterion, but direct evaluation is expensive: even reducing modulo after every multiplication still takes a number of multiplications proportional to , which grows exponentially with the number of binary digits of . The problem is not merely storing a huge factorial. The more useful role of Wilson's theorem is simplifying factorial expressions in modular arithmetic. For instance, if is a prime number, then gives by a modular multiplicative inverse.
For an odd prime number , put . Both and are divisible by . By Wilson's theorem, they are also congruent modulo , because each is modulo . The coprime integers therefore both divide their difference, giving the displayed integer congruence. This combines the prime-modulus factorial identity with an elementary divisibility constraint.
For an odd prime , the even residues are the negatives of the odd residues. If , then . Apply Wilson theorem to obtain the displayed congruence. The empty product for is one.
For a prime number and nonnegative integers , Wilson theorem gives by replacing the last factors of with . For odd , have the same parity; for , both signs have the same residue class. Thus , including the endpoint cases .
The congruence class of modulo a positive integer is . Two integers have the same class exactly when their difference is divisible by . Addition and multiplication of representatives descend to well-defined operations on , because replacing a representative by a multiple-of- translate changes the result by a multiple of .
For integers and a positive integer modulus , modular congruence meansThus for some integer : the two integers differ by complete cycles of length . For example, because , and because . A modular congruence need not be an equality of integers.
On a twelve-position clock labelled , adding five to nine follows . Hence : the final position remembers the remainder while forgetting a complete revolution.
The same idea tracks any count repeating every steps. It also lets us compute sums and products with small residue classes, reducing intermediate results without changing the final class. The justification comes from addition and multiplication being well-defined on residue classes.
In Disquisitiones arithmeticae (1801), Carl Friedrich Gauss defines modular congruence by divisibility in article 1; article 2 describes the integers and adopts . These passages document his presentation and notation, without claiming that repeating remainders began in 1801. [ref]
https://la.wikisource.org/wiki/Disquisitiones_arithmeticae/Sectio_prima. Gauss, Disquisitiones arithmeticae, section I, articles 1–2; original text in the 1870 collected edition
A quadratic congruence is a modular congruence defined by a degree-two polynomial, with solutions sought among residue classes. For odd and invertible modulo , completing the square gives . A Jacobi symbol of for a coprime proposed square rules out solutions, whereas a Jacobi symbol of alone does not establish solubility for a composite modulus.
For a prime and , write . The binomial theorem shows is divisible by : the linear term contains the extra factor and every higher term contains at least . Iteration gives the displayed modular congruence for every , including . Applied to Fermat's little theorem, it yields .
For a positive modulus , a reduced residue system contains exactly one representative of each residue class coprime to . Its size is the Euler totient function . Multiplication by any unit modulo n permutes these classes, which proves the Fermat-Euler theorem after multiplying and cancelling their unit product.
A linear congruence asks for the integers satisfying , where are integers and . Let be their greatest common divisor. There are solutions exactly when divides . When solutions exist, there are exactly solution classes modulo , obtained from one solution byThe divisibility condition follows immediately from : every common divisor of must divide . Conversely, Bézout's identity constructs a solution when . Differences between any two solutions obey , equivalently , since and are coprime. This explains both the spacing and the number of solution classes.
For a complete example, solve . Here divides nine. Dividing the corresponding divisibility statement by three changes the integer modulus too:Because , multiplying by the modular inverse three gives . Lifting this class back to modulus fifteen yields . Direct substitution gives , and , each with remainder nine after division with remainder by fifteen. By contrast, has no solutions, because three does not divide ten.
This is why ordinary-looking division cannot be performed blindly in modular arithmetic. Reducing the equation and its integer modulus together keeps all solutions; retaining modulus fifteen after dividing by three would lose valid classes. When , the modular inverse of exists and gives a unique class . The Euclidean algorithm computes the Bézout coefficients needed for this construction. [ref]
https://ocw.mit.edu/courses/18-200-principles-of-discrete-applied-mathematics-spring-2024/mit18_200_s24_lec15.pdf. Moitra, MIT 18.200, Lecture 15, modular inverses and the extended Euclidean algorithm
For integers and positive integers , a common factor may be cancelled from a integer congruence provided the modulus is divided by the corresponding greatest common divisor. Let , , and . The integers are coprime: any common divisor greater than one would give a common divisor of larger than .
By divisibility and Euclid's lemma,This proves the displayed equivalence. If is coprime to , the modulus stays unchanged and this is ordinary cancellation by a modular multiplicative inverse. If , the reduced modulus is , so every pair satisfies the reduced integer congruence.
For a positive integer modulus , the residue class of an integer is the equivalence classIt contains all integers in the same position of the repeating cycle. For example,The residue class is this whole infinite collection; its canonical representative is the least nonnegative residue. Euclidean division gives exactly one such representative in , so there are exactly residue classes. When , there is just one, .
Define addition and multiplication of residue classes byThese definitions must give the same answer whichever integers represent the classes. If and modulo , thenBoth differences are divisible by , proving that the operations are well-defined. For example, and , since . Replacing nine by gives the same answers with smaller intermediate integers.
With these operations the residue classes form the finite commutative ring , a quotient ring. It need not be a field: although neither factor is the zero class. These zero divisors explain why division requires a unit modulo n. Reducing after each multiplication is especially useful in modular exponentiation, where the unreduced powers can become enormous.
For a positive integer modulus , a residue class is a unit when it has a modular multiplicative inverse: some integer satisfies . Modulo twelve, the units are exactlyIn particular, five is its own modular inverse, since . Multiplying all twelve residue classes by five gives a permutation; multiplying the outputs by five recovers the inputs. By contrast, multiplying by six gives only zero and six, with six inputs for each output. It cannot be reversed.
Multiplication by five permutes all twelve residue classes, whereas multiplication by six merges distinct inputs
. Details. In general, the exact criterion isFor the forward direction, implies every common divisor of divides one. Conversely, if their greatest common divisor is one, Bézout's identity supplies , so is a modular inverse. The extended Euclidean algorithm computes these coefficients. The units form the unit group under multiplication. [ref]
https://ocw.mit.edu/courses/18-200-principles-of-discrete-applied-mathematics-spring-2024/mit18_200_s24_lec15.pdf. Ankur Moitra, MIT 18.200, Lecture 15 (2024), pp. 3–4: inverses, coprimality and the unit group
Multiplicative cancellation while keeping the same integer modulus is valid for a unit modulo n: from , multiply by a modular inverse of to get . A nonunit cannot be cancelled as a general rule. For example, , yet . Thus division by six cannot undo this multiplication; solving a linear congruence instead keeps track of all possible classes.
This distinction marks the boundary between commutative rings and fields. Modulo a prime number , every nonzero residue class is a unit, so is a finite field. Modulo twelve it is a commutative ring with zero divisors, and is not a field. For , the sole class satisfies and is a unit; this one-element zero ring is not a field. [ref]
https://ocw.mit.edu/courses/18-200-principles-of-discrete-applied-mathematics-spring-2024/mit18_200_s24_lec15.pdf. Moitra, MIT 18.200, Lecture 15, pp. 8–9: the prime-modulus field criterion
Articles by others on the same topic
Modular arithmetic, often referred to as "clock arithmetic," is a system of arithmetic for integers, where numbers wrap around after reaching a certain value known as the modulus. In modular arithmetic, two numbers are considered equivalent if they have the same remainder when divided by the modulus. The basic notation for modular arithmetic is \( a \equiv b \mod m \), which means that \( a \) and \( b \) give the same remainder when divided by \( m \).




