# Modular arithmetic

Time-keeping on this clock uses arithmetic modulo 12.

In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value—the modulus. The modern approach to modular arithmetic was developed by Carl Friedrich Gauss in his book Disquisitiones Arithmeticae, published in 1801.

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; in 12-hour time, there is no "15 o'clock". Likewise, if the clock starts at 12:00 (noon) and 21 hours elapse, then the time will be 9:00 the next day, rather than 33:00. 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.

## History

The foundations of modular arithmetic were introduced in the third century BCE, by Euclid, in the 7th book of his Elements.

## Congruence relation

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 integers a and b are said to be congruent modulo n, written:

$a \equiv b \pmod n,\,$

if their difference a − b is an integer multiple of n (or n divides a − b). The number n is called the modulus of the congruence.

For example,

$38 \equiv 14 \pmod {12}\,$

because 38 − 14 = 24, which is a multiple of 12.

The same rule holds for negative values:

\begin{align} -8 &\equiv 7 \pmod 5\\ 2 &\equiv -3 \pmod 5\\ -3 &\equiv -8 \pmod 5\, \end{align}

Equivalently, $a \equiv b \pmod n\,$ can also be thought of as asserting that the remainders of the division of both $a$ and $b$ by $n$ are the same. For instance:

$38 \equiv 14 \pmod {12}\,$

because both 38 and 14 have the same remainder 2 when divided by 12. It is also the case that $38 - 14 = 24$ is an integer multiple of 12, which agrees with the prior definition of the congruence 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 clearer if the notation a n b had been used, instead of the common traditional notation.

The properties that make this relation a congruence relation (respecting addition, subtraction, and multiplication) are the following.

If

$a_1 \equiv b_1 \pmod n$

and

$a_2 \equiv b_2 \pmod n,$

then:

• $a_1 + a_2 \equiv b_1 + b_2 \pmod n\,$
• $a_1 - a_2 \equiv b_1 - b_2 \pmod n\,$

The above two properties would still hold if the theory were expanded to include all real numbers, that is if $a_1, a_2, b_1, b_2, n\,$ were not necessarily all integers. The next property, however, would fail if these variables were not all integers:

• $a_1 a_2 \equiv b_1 b_2 \pmod n.\,$

## Remainders

The notion of modular arithmetic is related to that of the remainder in Euclidean division. The operation of finding the remainder is sometimes referred to as the modulo operation, and denoted with "mod" used as an infix operator. For example, the remainder of the division of 14 by 12 is denoted by 14 mod 12; as this remainder is 2, we have 14 mod 12 = 2.

The congruence, indicated by "≡" followed by "mod" between parentheses, means that the operator "mod", applied to both members, gives the same result. That is

$A\equiv B \pmod n$

is equivalent to

$A\!\!\!\!\mod n = B \!\!\!\!\mod n.$

The fundamental property of multiplication in modular arithmetic may thus be written

$(a \!\!\!\!\mod n)\,(b\!\!\!\!\mod n) \equiv ab \pmod n,$

or, equivalently,

$((a \!\!\!\!\mod n)\,(b\!\!\!\!\mod n)) \!\!\!\!\mod n = (ab) \!\!\!\!\mod n.$

In computer science, it is the remainder operator that is usually indicated by either "%" (e.g. in C, C++, Java, JavaScript, Perl and Python) or "mod" (e.g. in Pascal, BASIC, SQL, Haskell, ABAP), with exceptions (e.g. Excel). These operators are commonly pronounced as "mod", but it is specifically a remainder that is computed (since in C++ a negative number will be returned if the first argument is negative, and in Python a negative number will be returned if the second argument is negative). The function modulo instead of mod, like 38 ≡ 14 (modulo 12) is sometimes used to indicate the common residue rather than a remainder (e.g. in Ruby). For details of the specific operations defined in different languages, see the modulo operation page.

## Residue systems

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.

It is clear that the least residue system is a complete residue system, and that 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.

### Reduced residue systems

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.

## Congruence classes

Like any congruence relation, congruence modulo n is an equivalence relation, and the equivalence class of the integer a, denoted by $\overline{a}_n$, is the set $\left\{\ldots, a - 2n, a - n, a, a + n, a + 2n, \ldots \right\}$. 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 $\displaystyle [a]$.

## Integers modulo n

The set of all congruence classes of the integers for a modulus n is called the set of integers modulo n, and is denoted $\mathbb{Z}/n\mathbb{Z}$, $\mathbb{Z}/n$, or $\mathbb{Z}_n$. The notation $\mathbb{Z}_n$ is, however, not recommended because it can be confused with the set of n-adic integers. The set is defined as follows.

$\mathbb{Z}/n\mathbb{Z} = \left\{ \overline{a}_n | a \in \mathbb{Z}\right\}.$

When n ≠ 0, $\mathbb{Z}/n\mathbb{Z}$ has n elements, and can be written as:

$\mathbb{Z}/n\mathbb{Z} = \left\{ \overline{0}_n, \overline{1}_n, \overline{2}_n,\ldots, \overline{n-1}_n \right\}.$

When n = 0, $\mathbb{Z}/n\mathbb{Z}$ does not have zero elements; rather, it is isomorphic to $\mathbb{Z}$, since $\overline{a}_0 = \left\{a\right\}$.

We can define addition, subtraction, and multiplication on $\mathbb{Z}/n\mathbb{Z}$ by the following rules:

• $\overline{a}_n + \overline{b}_n = \overline{(a + b)}_n$
• $\overline{a}_n - \overline{b}_n = \overline{(a - b)}_n$
• $\overline{a}_n \overline{b}_n = \overline{(ab)}_n.$

The verification that this is a proper definition uses the properties given before.

In this way, $\mathbb{Z}/n\mathbb{Z}$ becomes a commutative ring. For example, in the ring $\mathbb{Z}/24\mathbb{Z}$, we have

$\overline{12}_{24} + \overline{21}_{24} = \overline{9}_{24}$

as in the arithmetic for the 24-hour clock.

The notation $\mathbb{Z}/n\mathbb{Z}$ is used, because it is the factor ring of $\mathbb{Z}$ by the ideal $n\mathbb{Z}$ containing all integers divisible by n, where $0\mathbb{Z}$ is the singleton set $\left\{0\right\}$. Thus $\mathbb{Z}/n\mathbb{Z}$ is a field when $n\mathbb{Z}$ is a maximal ideal, that is, when $n$ is prime.

In terms of groups, the residue class $\overline{a}_n$ is the coset of a in the quotient group $\mathbb{Z}/n\mathbb{Z}$, a cyclic group.[4]

The set $\mathbb{Z}/n\mathbb{Z}$ has a number of important mathematical properties that are foundational to various branches of mathematics.

Rather than excluding the special case n = 0, it is more useful to include $\mathbb{Z}/0\mathbb{Z}$ (which, as mentioned before, is isomorphic to the ring $\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. If n is a non-prime prime power, there exists a unique (up to isomorphism) finite field GF(n) with n elements, which must not be confused with the ring of integers modulo n, although they have the same number of elements.

## Applications

Modular arithmetic is referenced in number theory, group theory, ring theory, knot theory, abstract algebra, computer algebra, cryptography, computer science, chemistry and the visual and musical arts.

It is one of the foundations of number theory, touching on almost every aspect of its study, and provides key examples for group theory, ring theory and abstract algebra.

Modular arithmetic is often used to calculate checksums that are used within identifiers. International Bank Account Numbers (IBANs), for example, make use of modulo 97 arithmetic to trap user input errors in bank account numbers.

In cryptography, modular arithmetic directly underpins public key systems such as RSA and Diffie-Hellman, as well as providing finite fields which underlie elliptic curves, and is used in a variety of symmetric key algorithms including AES, IDEA, and RC4.

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.

In computer science, modular arithmetic is often applied in bitwise operations and other operations involving fixed-width, cyclic data structures. The modulo operation, as implemented in many programming languages and calculators, is an application of modular arithmetic that is often used in this context. XOR is the sum of 2 bits, modulo 2.

In chemistry, the last digit of the CAS registry number (a number which is unique 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 next digit times 2, the next digit times 3 etc., adding all these up and computing the sum modulo 10.

In music, arithmetic modulo 12 is used in the consideration of the system of twelve-tone equal temperament, where octave and enharmonic equivalency occurs (that is, pitches in a 1∶2 or 2∶1 ratio are equivalent, and C-sharp is considered the same as D-flat).

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.

More generally, modular arithmetic also has application in disciplines such as law (see e.g., apportionment), economics, (see e.g., game theory) and other areas of the social sciences, where proportional division and allocation of resources plays a central part of the analysis.

## Computational complexity

Since modular arithmetic has such a wide range of applications, it is important to know how hard it is to solve a system of congruences. A linear system of congruences can be solved in polynomial time with a form of Gaussian elimination, for details see linear congruence theorem. Algorithms, such as Montgomery reduction, also exist to allow simple arithmetic operations, such as multiplication and exponentiation modulo n, to be performed efficiently on large numbers.

Solving a system of non-linear modular arithmetic equations is NP-complete.[5]

## Example implementations

Below are two reasonably fast C functions for performing modular multiplication on unsigned integers not larger than 63 bits, without overflow of the transient operations. An algorithmic way to compute a * b (mod m):

uint64_t mul_mod(uint64_t a, uint64_t b, uint64_t m)
{
uint64_t d = 0, mp2 = m >> 1;
int i;
if (a >= m) a %= m;
if (b >= m) b %= m;
for (i = 0; i < 64; ++i)
{
d = (d >= mp2) ? (d << 1) - m : d << 1;
if (a & 0x8000000000000000ULL)
d += b;
if (d > m) d -= m;
a <<= 1;
}
return d;
}


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:

uint64_t mul_mod(uint64_t a, uint64_t b, uint64_t m)
{
long double x;
uint64_t c;
int64_t r;
if (a >= m) a %= m;
if (b >= m) b %= m;
x = a;
c = x * b / m;
r = (int64_t)(a * b - c * m) % (int64_t)m;
return r < 0 ? r + m : r;
}


However, for both routines to work, m must not exceed 63 bits.