Encyclopedia  |   World Factbook  |   World Flags  |   Reference Tables  |   List of Lists     
   Academic Disciplines  |   Historical Timeline  |   Themed Timelines  |   Biographies  |   How-Tos     
Sponsor by The Tattoo Collection
Modular arithmetic
Main Page | See live article | Alphabetical index

Modular arithmetic

In mathematics, modular arithmetic is a system of arithmetic for certain equivalence classes of integers, called congruence classes. Sometimes it is suggestively called 'clock arithmetic', where numbers 'wrap around' after they reach a certain value (the modulus). For example, when the modulus is 12, then any two numbers that leave the same remainder when divided by 12 are equivalent (or "congruent") to each other. The numbers

..., −34, −22, −10, 2, 14, 26, ...

are all "congruent modulo 12" to each other, because each leaves the same remainder (2) on division by 12. The collection of all such numbers is a congruence class.

As explained below, one can add such congruence classes to get another such congruence class, subtract two such classes to get another, and multiply such classes to get another. When the modulus is a prime number, one can always divide by any class not containing 0.

Table of contents
1 Definition of modulo
2 Applications of modular arithmetic
3 Some consequences of the mathematical usage
4 Another "computing" usage
5 More general use of the word modulo by mathematicians
6 Slang use of the word modulo
7 External resources

Definition of modulo

Two discrepant conventions prevail:

A third sort of usage by mathematicians is quite different from these, but does not conflict with them because it deals with different subject matter. It evolved ultimately from the usage introduced by Gauss in 1801. It is explained in the article titled modulo.

The older convention, used by mathematicians

The original convention is that the expression

means that a and b are both in the same "congruence class" modulo n, i.e., both leave the same remainder on division by n, or, equivalently, ab is a multiple of n. Thus we have, for example

since 63 and 83 both leave the same remainder (3) on division by 10, or, equivalently, 63 − 83 is a multiple of 10. One says:

"63 is congruent to 83, modulo 10,"


"63 and 83 are congruent to each other, modulo 10."

"Modulo" is usually abbreviated to "mod" in speaking, just as in writing. The parentheses, i.e., the round brackets (), are usually not written, but in this case they emphasize the difference between the traditional mathematical convention and the modern computing convention. The mathematical usage parses the phrase differently from the computing usage.

In Latin, the language in which Gauss wrote, modulo is the ablative case of modulus. The number n, which in this example is 10, is the modulus.

The newer convention, used in computing

According to the newer convention, in general, a mod n is the remainder on integer division of a by n. Depending on the implementation, the remainder r is typically constrained to 0 < |r| < |n|, with a negative remainder only resulting when n < 0.

The difference in conventions is not very serious, in fact; it is reasonably thought of as reflecting the preference, for computational purposes, of a normal form over the underlying equivalence relation. This can be regarded mainly as a notational convention in this case, where there is a strict-sense normal form.

Implementation of modulo in computing

Some calculators have a mod() function button, and many programming languages have a mod() function or similar, expressed as mod(a,n), for example. Some also support expressions that use "%" as a modulo operator, such as a % n.

a mod n can be calculated by using equations, in terms of other functions. Differences may arise according to the scope of the variables, which in common implementations is broader than in the definition just given.

An implementation of a modulo function that constrains the remainder set in the manner described above, as is found in the programming languages Perl and Python, can be described in terms of the floor function floor(z), the greatest integer less than or equal to z:

mod(a,n) = an × floor(a ÷ n)

This definition allows for a and n to be typed as integers or rational numbers.

The expression a mod 0 is undefined in the majority of numerical systems, although some do define it to be n.

Applications of modular arithmetic

Modular arithmetic, first systematically studied by Carl Friedrich Gauss at the end of the eighteenth century, is applied in number theory, abstract algebra, cryptography, and visual and musical art.

The fundamental arithmetic operations performed by most computers are actually modular arithmetic, where the modulus is 2b (b being the number of bits of the values being operated on). This comes to light in the compilation programming languages such as C; where for example arithmetic operations on "int" integers are all taken modulo 232, on most computers.

In art

In music, because of octave and enharmonic equivalency (that is, pitches in a 1/2 or 2/1 ratio are equivalent, and C# is the same as Db), modular arithmetic is used in the consideration of the twelve tone equally tempered scale, especially in twelve tone music. In visual art modular arithmetic can be used to create artistic patterns based on the multiplication and addition tables modulo n (see link below).

Some consequences of the mathematical usage

Recall from above that two integers a, b congruent modulo n, written as

ab (mod n) if their difference ab is divisible by n, i.e. if ab = kn for some integer k.

Using this definition, we can generalize to non-integral moduli. For instance, we can define ab (mod π) if ab = kπ for some integer k. This idea is developed in full in the context of ring theory below.

Here is an example of the congruence notation.

14 ≡ 26 (mod 12).
This is an equivalence relation, and the equivalence class of the integer a is denoted by [a]n (or simply [a] if the modulus n is understood.) Other notations include a + nZ or a\ mod n. The set of all equivalence classes is denoted Z/nZ = { [0]n, [1]n, [2]n, ..., [n-1]n }.

If a and b are integers, the congruence

axb (mod n)
has a solution x if and only if the greatest common divisor (a, n) divides b. The details are recorded in the linear congruence theorem. More complicated simultaneous systems of congruences with different moduli can be solved using the Chinese remainder theorem or the method of successive substitution.

This equivalence relation has important properties which follow immediately from the definition: if

a1b1 (mod n)    and    a2b2 (mod n)
a1 + a2b1 + b2 (mod n)
a1a2b1b2 (mod n).

This shows that addition and multiplication are well-defined operations on the set of equivalence classes. In other words, addition and multiplication are defined on Z/nZ by the following formulae: In this way, Z/nZ becomes a commutative ring with n elements. For instance, in the ring Z/12Z, we have
[8]12[3]12 + [6]12 = [30]12 = [6]12.

In abstract algebra, it is realized that modular arithmetic is a special case of forming the factor ring of a ring modulo an ideal. If R is a commutative ring, and I is an ideal of R, then the elements a and b of R are congruent modulo I if ab is an element of I. As with the ring of integers, this turns out to be an equivalence relation, and addition and multiplication become well-defined operations on the factor ring R/I.

In the ring of integers, if we consider the equation ax ≡ 1 (mod n), then we see that a has a multiplication inverse if and only if a and n are coprime. Therefore, Z/nZ is a field if and only if n is prime. It can be shown that every finite field is an extension of Z/pZ for some prime p.

An important fact about prime number moduli is Fermat's little theorem: if p is a prime number and a is any integer, then

apa (mod p).
This was generalized by Euler: for any positive integer n and any integer a that is relatively prime to n,
aφ(n) ≡ 1 (mod n),
where φ(n) denotes Euler's φ function; counting the integers between 1 and n that are coprime to n. Euler's theorem is a consequence of the Theorem of Lagrange, applied to the group of units of the ring Z/nZ.

Another "computing" usage

An implied meaning of modulo in computing contexts is "valid up to this value." For example, "addition is modulo 1,000" means that the addition operation being described provides valid answers until the sum goes beyond 1,000.

Digital representations of number spaces are not infinite (see binary numeral systems). Thus, if a computer is representing a set of positive integers as 8-bits, the values that can be represented range from 0 to 255. When an addition (or multiplication, or whatever) results in a number above this cutoff, the typical behavior is for the values to wrap around. For example, in the 8-bit positive integer situation, 255 + 1 = 0. This computer is therefore described as "modulo 256". Furthermore, some computers do different operations with different bit representations. So although the storage of integers may be 8-bit ("modulo 256"), the addition of integers may be 12-bit ("modulo 4096"), or anything else. Thus individual operations can also be described as "modulo x".

In the case of signed (positive and negative) integers, or floating point numbers, the wrap around effect is more complicated, and is not always perfectly analogous to the formal mathematical modulo. However, the slang persists such that "addition is modulo 1000" may not literally mean (in fact cannot literally mean) that the computer does addition in bits, but may simply mean "watch out: if you go over 1000 this computer will give you weird results".

More general use of the word modulo by mathematicians

To say that any two things are the same "modulo" a third thing means, more-or-less, that the difference between the first two is accounted for or explained by the third. That is, the up to concept is often talked about this way, using modulo as a term alerting the hearer. In mathematics, this admits various precise definitions. In particular, two members of a ring or an algebra are congruent modulo an ideal if the difference between them is in the ideal. The use of the term in modular arithmetic is a special case of that usage, and that is how this more general usage evolved. Some loose terms such as almost all can in this way acquire precise meanings from a Boolean algebra version, in which symmetric difference of sets replaced arithmetical subtraction; for example "modulo finite sets".

See modulo.

Slang use of the word modulo

Mathematicians speaking of things non-mathematical still say "A is the same as B modulo C" when they mean A is the same as B except for differences accounted for by C. But in such non-mathematical contexts, the phrase may not admit any very precise definition. Consequently mathematicians and computer scientists often use the phrase in an informal or even jocular way.

Some users of the term either lack this theoretical viewpoint or else ignore it, and use the word "modulo" more-or-less synonymously with the preposition except.


"http and https are the same, modulo encryption." - means "the only difference between http and https is the addition of encryption".

"These two characters are equal."
"You mean, equal modulo case." - indicates that the first speaker is wrong: the characters are not the same, one is uppercase and the other lowercase.

"The two students performed equally well on the exam, modulo some minor computational mistakes." - means that the two students demonstrated an equal understanding of the material and its application, but one of them lost some points for minor computational mistakes.

"This code is finished modulo testing" - means "this code is finished except for testing". Since testing is generally considered quite important, whereas in mathematics the use of modular arithmetic generally ignores the difference between modulo-equal numbers, use of a phrase like this might be deliberate understatement.

External resources