Modular arithmetic and residue classes
The mod operator for positive and negative integers, congruence modulo n, the addition, subtraction and multiplication properties, residue classes in Zn, and a full worked modular exponentiation.
- Compute a mod n for negative and positive a, including the case -11 mod 7 = 3.
- State when two integers are congruent modulo n and check a given pair.
- Verify the addition, subtraction and multiplication properties of modular arithmetic against a worked numeric example.
- List the residue classes of Zn and compute a modular exponentiation step by step.
15 min readJump to the quiz ↓
Mod wraps every integer into a fixed range, and the remainder is never negative
Every cryptographic scheme in this unit computes inside a fixed-size set of numbers. RSA ciphertexts stay below the modulus , and Diffie-Hellman keys stay below a prime . Without a boundary, encrypting even a short message would produce numbers thousands of digits long. The boundary is enforced by one operation, mod: values wrap around like a clock instead of growing forever.
For an integer and a positive integer , is the remainder when is divided by . It is the division algorithm from the previous page, with and , now extended to integers that can be negative. The remainder is always non-negative, whatever the sign of .
For , : , so .
For , : , so .
Pitfall
rounds toward negative infinity, not toward zero. For , truncating toward zero gives and a remainder of , which is negative and outside the required range . The two rounding rules agree for non-negative and disagree the moment goes negative. Every mod result on this site, and every mod result the exam expects, is non-negative.
Recall
Why does -11 mod 7 give 3, and not -4?
Because the definition requires . must be , the floor, not , the truncation toward zero. , which satisfies the range; does not.
Congruent numbers share a remainder, so you can reduce before you combine
Two integers and are congruent modulo if they leave the same remainder: . This is written . Congruence does not require and to be close, equal, or both positive, only that dividing each by leaves the same remainder. One consequence: exactly when .
, because and . , because and , the same remainder even though and are ten apart. Checking congruence never requires comparing and directly, only their two remainders.
Formula
Congruence modulo n
- any integers
- the modulus
a ≡ 0 (mod n) if and only if n divides a.
Ordinary arithmetic properties carry over. For any integers , and modulus :
Reducing first and then combining gives the same answer as combining first and then reducing. This is what lets implementations keep every intermediate value small: reduce mod at every step instead of computing the full-size result and reducing once at the end.
Try it — worked example
With a = 11, b = 9, n = 8, check that modular addition, subtraction and multiplication give the same result whether you reduce first or last.
Show answer and steps
AnswerAll three properties hold for a = 11, b = 9, n = 8
The lecture verifies each property with the same pair, , , . First, and .
- Addition. LHS: . RHS: . LHS = RHS.
- Subtraction. LHS: . RHS: . LHS = RHS.
- Multiplication. LHS: . RHS: , since and . LHS = RHS.
Z_n holds one representative per residue class, and powers are built by reducing as you go
Define as the set of non-negative integers less than : . Every integer, not only the ones in this range, falls into one of these residue classes. The class is the set of every integer congruent to modulo : lists the smallest non-negative member of each class, which is why always lands inside . Arithmetic on is the modular arithmetic above: add, subtract or multiply the representatives and reduce mod to land back inside the set.
Exponentiation is repeated multiplication, so the same reduce-as-you-go idea applies. Compute by building the power through smaller modular multiplications instead of computing the full power first. This one calculation is the basis of Diffie-Hellman key exchange in Module 6.
Try it — worked example
Compute 11^7 mod 13 without calculating 11^7 in full.
Show answer and steps
Answer11^7 mod 13 = 2
- . , since and .
- . .
- Write as , since .
- . , and .
- : , and . So .
Pitfall
The Week 4 abstract-algebra supplement uses to show that has no multiplicative inverse
modulo , and its printed working states . That line is wrong: , and
, not . The corrected table of for is
. Every result is even, so none is , and the conclusion (no inverse exists) still
holds. 04-06 returns to this table as the counter-example for why is a commutative ring but
not a field.
Check yourself
Source
Week 4 Number Theory PDF