A familiar use of modular arithmetic is in the 12-hour clock, in which the day is divided into two 12-hour periods. If the time is 7:00 now, then 8 hours later it will be 3:00. Usual addition would suggest that the later time should be 7 + 8 = 15, but this is not the answer because clock time "wraps around" every 12 hours. Because the hour number starts over after it reaches 12, this is arithmetic modulo 12. According to the definition below, 12 is congruent not only to 12 itself, but also to 0, so the time called "12:00" could also be called "0:00", since 12 is congruent to 0 modulo 12.

Contents

This section is about the (mod n) notation. For the binary mod operation, see modulo operation.

Modular arithmetic can be handled mathematically by introducing a congruence relation on the integers that is compatible with the operations on integers: addition, subtraction, and multiplication. For a positive integer n, two numbers a and b are said to be congruent modulo n, if their difference a − b is an integer multiple of n (that is, if there is an integer k such that a − b = kn). This congruence relation is typically considered when a and b are integers, and is denoted

a≡b(modn).{\displaystyle a\equiv b{\pmod {n}}.}

(some authors use = instead of ≡; in this case, if the parentheses are omitted, this generally means that "mod" denotes the modulo operation, that is, that 0 ≤ a < n).

The number n is called the modulus of the congruence.

The congruence relation may be rewritten as

a=kn+b,{\displaystyle a=kn+b,}

explicitly showing its relationship with Euclidean division. However, b need not be the remainder of the division of a by n. More precisely, what the statement a ≡ b mod n asserts is that a and b have the same remainder when divided by n. That is,

a=pn+r,{\displaystyle a=pn+r,}

b=qn+r,{\displaystyle b=qn+r,}

where 0 ≤ r < n is the common remainder. Subtracting these two expressions, we recover the previous relation:

A remark on the notation: Because it is common to consider several congruence relations for different moduli at the same time, the modulus is incorporated in the notation. In spite of the ternary notation, the congruence relation for a given modulus is binary. This would have been more direct if the notation a ≡nb had been used, instead of the common traditional notation.

A simple consequence of Fermat's little theorem is that if p is prime, then a−1 ≡ ap − 2 (mod p) is the multiplicative inverse of 0 < a < p. More generally, from Euler's theorem, if a and n are coprime, then a−1 ≡ aφ(n) − 1 (mod n).

Chinese remainder theorem: If x ≡ a (mod m) and x ≡ b (mod n) such that m and n are coprime, then x ≡ b mn–1m + a nm–1n (mod mn) where mn−1 is the inverse of m modulo n and nm−1 is the inverse of n modulo m

Lagrange's theorem: The congruence f (x) ≡ 0 (mod p), where p is prime, and f (x) = a0xn + ... + an is a polynomial with integer coefficients such that a0 ≠ 0 (mod p), has at most n roots.

Primitive root modulo n: A number g is a primitive root modulo n if, for every integer a coprime to n, there is an integer k such that gk ≡ a (mod n). A primitive root modulo n exists if and only if n is equal to 2, 4, pk or 2pk, where p is an odd prime number and k is a positive integer. If a primitive root modulo n exists, then there are exactly φ(φ(n)) such primitive roots, where φ is the Euler's totient function.

Quadratic residue: An integer a is a quadratic residue modulo n, if there exists an integer x such that x2 ≡ a (mod n). Euler's criterion asserts that, if p is an odd prime, and a is not a multiple of p, then a is a quadratic residue modulo p if and only if

Like any congruence relation, congruence modulo n is an equivalence relation, and the equivalence class of the integer a, denoted by an, is the set {… , a − 2n, a − n, a, a + n, a + 2n, …}. This set, consisting of the integers congruent to a modulo n, is called the congruence class or residue class or simply residue of the integer a, modulo n. When the modulus n is known from the context, that residue may also be denoted [a].

Each residue class modulo n may be represented by any one of its members, although we usually represent each residue class by the smallest nonnegative integer which belongs to that class (since this is the proper remainder which results from division). Any two members of different residue classes modulo n are incongruent modulo n. Furthermore, every integer belongs to one and only one residue class modulo n.[1]

The set of integers {0, 1, 2, …, n − 1} is called the least residue system modulo n. Any set of n integers, no two of which are congruent modulo n, is called a complete residue system modulo n.

The least residue system is a complete residue system, and a complete residue system is simply a set containing precisely one representative of each residue class modulo n.[2] The least residue system modulo 4 is {0, 1, 2, 3}. Some other complete residue systems modulo 4 are:

{1, 2, 3, 4}

{13, 14, 15, 16}

{−2, −1, 0, 1}

{−13, 4, 17, 18}

{−5, 0, 6, 21}

{27, 32, 37, 42}

Some sets which are not complete residue systems modulo 4 are:

{−5, 0, 6, 22} since 6 is congruent to 22 modulo 4.

{5, 15} since a complete residue system modulo 4 must have exactly 4 incongruent residue classes.

Any set of φ(n) integers that are relatively prime to n and that are mutually incongruent modulo n, where φ(n) denotes Euler's totient function, is called a reduced residue system modulo n.[3] The example above, {5,15} is an example of a reduced residue system modulo 4.

The set of all congruence classes of the integers for a modulus n is called the ring of integers modulo n,[4] and is denoted Z/nZ{\displaystyle \mathbb {Z} /n\mathbb {Z} }, Z/n{\displaystyle \mathbb {Z} /n}, or Zn{\displaystyle \mathbb {Z} _{n}}. The notation Zn{\displaystyle \mathbb {Z} _{n}} is, however, not recommended because it can be confused with the set of n-adic integers. The ring Z/nZ{\displaystyle \mathbb {Z} /n\mathbb {Z} } is fundamental to various branches of mathematics (see Applications below).

We use the notation Z/nZ{\displaystyle \mathbb {Z} /n\mathbb {Z} } because this is the quotient ring of Z{\displaystyle \mathbb {Z} } by the idealnZ{\displaystyle n\mathbb {Z} } containing all integers divisible by n, where 0Z{\displaystyle 0\mathbb {Z} } is the singleton set{0}. Thus Z/nZ{\displaystyle \mathbb {Z} /n\mathbb {Z} } is a field when nZ{\displaystyle n\mathbb {Z} } is a maximal ideal, that is, when n is prime.

This can also be constructed from the group Z{\displaystyle \mathbb {Z} } under the addition operation alone. The residue class an is the group coset of a in the quotient groupZ/nZ{\displaystyle \mathbb {Z} /n\mathbb {Z} }, a cyclic group.[5]

Rather than excluding the special case n = 0, it is more useful to include Z/0Z{\displaystyle \mathbb {Z} /0\mathbb {Z} } (which, as mentioned before, is isomorphic to the ring Z{\displaystyle \mathbb {Z} } of integers), for example, when discussing the characteristic of a ring.

The ring of integers modulo n is a finite field if and only if n is prime. (this ensures every nonzero element has a multiplicative inverse). If n=pk{\displaystyle n=p^{k}} is a prime power with k > 1, there exists a unique (up to isomorphism) finite field GF(n)=Fn{\displaystyle \mathrm {GF} (n)=\mathbb {F} _{n}} with n elements, but this is notZ/nZ{\displaystyle \mathbb {Z} /n\mathbb {Z} }, which fails to be a field because it has zero-divisors.

We denote the multiplicative subgroup of the modular integers by (Z/nZ)×{\displaystyle (\mathbb {Z} /n\mathbb {Z} )^{\times }}. This consists of a¯n{\displaystyle {\overline {a}}_{n}} for acoprime to n, which are precisely the classes possessing a multiplicative inverse. This forms a commutative group under multiplication, with order φ(n){\displaystyle \varphi (n)}.

A very practical application is to calculate checksums within serial number identifiers. For example, International Standard Book Number (ISBN) uses modulo 11 (if issued before 1 January, 2007) or modulo 10 (if issued on or after 1 January, 2007) arithmetic for error detection. Likewise, International Bank Account Numbers (IBANs), for example, make use of modulo 97 arithmetic to spot user input errors in bank account numbers. In chemistry, the last digit of the CAS registry number (a unique identifying number for each chemical compound) is a check digit, which is calculated by taking the last digit of the first two parts of the CAS registry number times 1, the previous digit times 2, the previous digit times 3 etc., adding all these up and computing the sum modulo 10.

In computer algebra, modular arithmetic is commonly used to limit the size of integer coefficients in intermediate calculations and data. It is used in polynomial factorization, a problem for which all known efficient algorithms use modular arithmetic. It is used by the most efficient implementations of polynomial greatest common divisor, exact linear algebra and Gröbner basis algorithms over the integers and the rational numbers.

The method of casting out nines offers a quick check of decimal arithmetic computations performed by hand. It is based on modular arithmetic modulo 9, and specifically on the crucial property that 10 ≡ 1 (mod 9).

Arithmetic modulo 7 is used in algorithms that determine the day of the week for a given date. In particular, Zeller's congruence and the Doomsday algorithm make heavy use of modulo-7 arithmetic.

Below are three reasonably fast C functions, two for performing modular multiplication and one for modular exponentiation on unsigned integers not larger than 63 bits, without overflow of the transient operations.

An algorithmic way to compute a⋅b(modm){\displaystyle a\cdot b{\pmod {m}}}:

On computer architectures where an extended precision format with at least 64 bits of mantissa is available (such as the long double type of most x86 C compilers), the following routine is faster than any algorithmic solution, by employing the trick that, by hardware, floating-point multiplication results in the most significant bits of the product kept, while integer multiplication results in the least significant bits kept: