Skip to content

Modular Arithmetic

Updated · Equations updated; Sections updated; Examples or practice updated; Explanations expanded What changed?
  1. — Equations updated; Sections updated; Examples or practice updated; Explanations expanded

For a positive integer mm, write a≡b(modm)a\equiv b\pmod m when m∣(a−b)m\mid(a-b). The integers a,ba,b have the same remainder after division by mm. Congruences let us discard information that is irrelevant to a divisibility or remainder question.

We can add, subtract, and multiply congruences with the same modulus. We can also raise them to nonnegative integer powers. Reducing intermediate results keeps calculations small.

Example. Find the remainder when 720267^{2026} is divided by 1010.

The last digits of successive powers of seven are 7,9,3,17,9,3,1, then repeat because 74≡1(mod10)7^4\equiv1\pmod{10}. Since 2026≡2(mod4)2026\equiv2\pmod4,

72026≡72≡9(mod10).7^{2026}\equiv7^2\equiv9\pmod{10}.

A table suggests a cycle; an identity such as 74≡17^4\equiv1 proves the repetition. For longer exponents, repeated squaring is another efficient method.


A modular inverse of aa modulo mm is an integer uu with au≡1(modm)au\equiv1\pmod m. It exists exactly when gcd⁡(a,m)=1\gcd(a,m)=1, by Bézout’s identity.

Example. Solve 7x≡5(mod26)7x\equiv5\pmod{26}.

The Euclidean algorithm gives 1=3⋅26−11⋅71=3\cdot26-11\cdot7, so 7−1≡−11≡15(mod26)7^{-1}\equiv-11\equiv15\pmod{26}. Multiplying by the inverse,

x≡15⋅5≡23(mod26).x\equiv15\cdot5\equiv23\pmod{26}.

Check: 7⋅23=161≡5(mod26)7\cdot23=161\equiv5\pmod{26}.

If d=gcd⁡(a,m)d=\gcd(a,m), the congruence ax≡b(modm)ax\equiv b\pmod m has solutions exactly when d∣bd\mid b. In that case, divide the coefficients and the modulus by dd, then solve the reduced congruence. There are dd solution classes modulo the original modulus.

Example. Solve 6x≡8(mod14)6x\equiv8\pmod{14}.

Divide all three numbers by two to get 3x≡4(mod7)3x\equiv4\pmod7. Since 3−1≡5(mod7)3^{-1}\equiv5\pmod7, we have x≡6(mod7)x\equiv6\pmod7. Modulo 1414 this gives x≡6x\equiv6 or 1313.

Canceling the six while keeping modulus 1414 would not be valid because six has no inverse modulo fourteen.


For coprime positive moduli m,nm,n, any pair of congruences

x≡a(modm),x≡b(modn)x\equiv a\pmod m,\qquad x\equiv b\pmod n

has exactly one solution class modulo mnmn. A practical way to find it is to write x=a+mtx=a+mt and substitute into the other congruence.

Example. Find the smallest positive integer satisfying x≡2(mod5)x\equiv2\pmod5 and x≡3(mod7)x\equiv3\pmod7.

Write x=2+5tx=2+5t. The second congruence becomes 5t≡1(mod7)5t\equiv1\pmod7, so t≡3(mod7)t\equiv3\pmod7. Therefore x≡17(mod35)x\equiv17\pmod{35}, and the smallest positive solution is 1717.

For noncoprime moduli, compatibility is required: a≡b(modgcd⁡(m,n))a\equiv b\pmod{\gcd(m,n)}. Compatible systems have one class modulo lcm⁡(m,n)\operatorname{lcm}(m,n). For instance, x≡1(mod4)x\equiv1\pmod4 and x≡2(mod6)x\equiv2\pmod6 are incompatible because they force opposite parities.


The base-bb numeral with digits dk⋯d0d_k\cdots d_0 represents N=∑i=0kdibiN=\sum_{i=0}^k d_ib^i, where 0≤di<b0\le d_i<b. Modulo b−1b-1, the base is one, so NN is congruent to its digit sum. Modulo b+1b+1, the base is negative one, so NN is congruent to its alternating digit sum.

Example. Find the digit dd for which the decimal number 47d247d2 is divisible by 99.

The digit sum is 4+7+d+2=13+d4+7+d+2=13+d. This must be divisible by nine. Since 0≤d≤90\le d\le9, the only possibility is 13+d=1813+d=18, so d=5d=5.

To specify the last two digits in base bb, work modulo b2b^2. For decimal endings, that means modulo 100100, not modulo 1010.


A congruence obstruction is enough to prove no integer solution exists. First list possible residues of the powers involved, then compare with the required residue.

Example. Prove that x2+y2=4z+3x^2+y^2=4z+3 has no integer solutions.

An integer square is 00 or 11 modulo four. The sum of two squares can therefore be 0,1,0,1, or 22, but never 33 modulo four. The right side is always 33 modulo four, giving a contradiction.

Why Linear Congruences Have Several Solutions

Section titled “Why Linear Congruences Have Several Solutions”

Suppose ax≡b(modm)ax\equiv b\pmod m and d=gcd⁡(a,m)d=\gcd(a,m). The equivalent equation ax−b=mtax-b=mt shows that d∣bd\mid b is necessary. If it holds, divide by dd to obtain a congruence modulo m/dm/d with an invertible coefficient.

A single solution class modulo m/dm/d becomes dd classes modulo mm, separated by m/dm/d. This explains the solution count and the change of modulus at the same time.

Example. Find all residue classes modulo sixty satisfying 18x≡24(mod60)18x\equiv24\pmod{60}.

The GCD is six, which divides twenty-four. Divide to get 3x≡4(mod10)3x\equiv4\pmod{10}. The inverse of three modulo ten is seven, so x≡8(mod10)x\equiv8\pmod{10}.

The six classes modulo sixty are

8, 18, 28, 38, 48, 58.8,\ 18,\ 28,\ 38,\ 48,\ 58.

Each differs from the next by ten, not sixty. Substituting the first gives 18⋅8−24=12018\cdot8-24=120, and adding ten to xx changes the left side by a multiple of sixty.

If the GCD did not divide the right side, there would be no solution at all. Checking this condition before finding an inverse avoids an impossible calculation.


For pairwise coprime moduli m1,…,mkm_1,\ldots,m_k, let M=∏imiM=\prod_i m_i and Mi=M/miM_i=M/m_i. Since MiM_i is invertible modulo mim_i, choose uiu_i with Miui≡1(modmi)M_i u_i\equiv1\pmod{m_i}. Then

x=∑iaiMiuix=\sum_i a_iM_iu_i

satisfies x≡ai(modmi)x\equiv a_i\pmod{m_i} for every ii. Each term is zero modulo all the other moduli and has the desired value modulo its own modulus.

Uniqueness modulo MM follows because the difference of two solutions is divisible by every coprime modulus and hence by their product. This proof explains both parts of CRT rather than treating it as a search rule.

Example. Solve x≡1(mod3)x\equiv1\pmod3, x≡2(mod4)x\equiv2\pmod4, and x≡3(mod5)x\equiv3\pmod5.

Here M=60M=60. The complementary products are twenty, fifteen, and twelve, whose inverses modulo their respective moduli are two, three, and three. Therefore

x≡1(20)(2)+2(15)(3)+3(12)(3)=238≡58(mod60).x\equiv1(20)(2)+2(15)(3)+3(12)(3)=238\equiv58\pmod{60}.

The least nonnegative solution is fifty-eight. Direct substitution checks the three remainders.

For a system with noncoprime moduli, compare the required residues modulo their GCD first. Compatible systems can be merged two at a time, using the least common multiple as the new modulus. The product modulus is correct only in the coprime case.


Euler’s theorem cannot reduce an exponent when the base shares a factor with the modulus. A useful replacement is to split the modulus into coprime prime-power factors. On a factor dividing the base, a sufficiently large power becomes zero. On the remaining factors, ordinary order or Euler reduction is available.

Example. Find the last three decimal digits of 1210012^{100}.

Work modulo eight and one hundred twenty-five. The power is zero modulo eight because twelve contains two factors of two and the exponent is large enough.

Modulo 125125, the base is coprime and φ(125)=100\varphi(125)=100. Euler gives 12100≡1(mod125)12^{100}\equiv1\pmod{125}. Write x=1+125tx=1+125t. The condition modulo eight becomes 1+5t≡01+5t\equiv0, giving t≡3(mod8)t\equiv3\pmod8.

Thus x≡376(mod1000)x\equiv376\pmod{1000}, so the final three digits are 376.

This method avoids the tempting but invalid calculation of reducing the exponent modulo φ(1000)\varphi(1000) before checking the base. CRT separates the part where the power dies from the part where it cycles.


Modulo a prime, a product is zero only if a factor is zero. Modulo a composite number, zero divisors invalidate that step. Thus x2≡1(modm)x^2\equiv1\pmod m need not imply x≡1x\equiv1 or −1(modm)-1\pmod m.

Example. Find all solutions to x2≡1(mod15)x^2\equiv1\pmod{15}.

Modulo three, xx is one or negative one. Modulo five, it is also one or negative one. The four sign combinations are independent and give, by CRT,

x≡1, 4, 11, 14(mod15).x\equiv1,\ 4,\ 11,\ 14\pmod{15}.

For instance, 42−1=154^2-1=15, even though neither 4−14-1 nor 4+14+1 is divisible by fifteen. The factors split the prime divisors between them.

For an odd squarefree modulus with rr distinct prime factors, this argument gives 2r2^r square roots of one. Prime powers and powers of two require separate analysis; the squarefree statement should not be extended without checking those cases.

For odd prime powers, the two factors x−1,x+1x-1,x+1 have GCD dividing two. An odd prime cannot divide both, so its entire required power must lie in one factor. This explains why there are only two roots of one modulo an odd prime power.


Digit tests come from the base’s residue, not from decimal notation itself. In base bb, the value is congruent to the digit sum modulo b−1b-1 and to the alternating digit sum modulo b+1b+1. Also, the last kk digits determine the remainder modulo bkb^k.

Example. The base-bb numeral with digits 3, 4, 5 is divisible by b−1b-1. Find every possible base.

The digits require b≥6b\ge6. Since 345b=3b2+4b+5≡12(modb−1)345_b=3b^2+4b+5\equiv12\pmod{b-1}, the integer b−1b-1 must divide twelve and be at least five. It is therefore six or twelve, giving b=7b=7 or 1313.

Both bases satisfy the digit restriction and the divisibility condition.

A repeated-digit number often has a quotient representation, such as Rn=(10n−1)/9R_n=(10^n-1)/9 for a string of nn decimal ones. Do not divide a congruence by nine modulo a multiple of three. The quotient is an integer, but nine does not have a modular inverse there.

For example, 103−110^3-1 is divisible by twenty-seven, but R3=111R_3=111 is not. To prove m∣Rnm\mid R_n by clearing this quotient, require 9m∣10n−19m\mid10^n-1, not merely m∣10n−1m\mid10^n-1.


A residue obstruction may become visible only after removing forced factors. If three squares sum to a multiple of four, each square must be zero modulo four: with only three terms, a sum of residues zero or one cannot otherwise be zero.

Thus every variable is even, and the equation can be divided by four. Repeating this step strips powers of four before testing modulo eight.

Example. Prove that no integer of the form 4a(8b+7)4^a(8b+7), with a,b≥0a,b\ge0, can be a sum of three integer squares.

Suppose x2+y2+z2=4a(8b+7)x^2+y^2+z^2=4^a(8b+7). If a>0a>0, the preceding modulo-four argument forces all variables even. Divide by four and repeat until the right side is 8b+78b+7.

A square modulo eight is zero, one, or four. Three such residues cannot sum to seven: with no four the sum is at most three; with one four the other two give residues zero, one, or two; with at least two fours the remaining residue gives zero, one, or four modulo eight. This is a contradiction.

The argument proves a necessary obstruction, not the full classification of sums of three squares. Keep that distinction when using a modular proof to infer existence.


Lifting a Square Root One Prime Power at a Time

Section titled “Lifting a Square Root One Prime Power at a Time”

Suppose x02≡a(modp)x_0^2\equiv a\pmod p. Every lift of this residue modulo p2p^2 has the form x=x0+ptx=x_0+pt. Expanding and dividing the known multiple of pp gives

x02−ap+2x0t≡0(modp).\frac{x_0^2-a}{p}+2x_0t\equiv0\pmod p.

If p∤2x0p\nmid2x_0, this is a linear congruence with one solution for tt modulo pp. Thus each nondegenerate root has a unique lift. This is an elementary instance of Hensel lifting, distinct from LTE’s valuation formula.

Example. Find all square roots of two modulo forty-nine.

Modulo seven, the roots are three and four. Lift three by writing x=3+7tx=3+7t. Then x2−2=7+42t+49t2x^2-2=7+42t+49t^2, so 1+6t≡0(mod7)1+6t\equiv0\pmod7. This gives t=1t=1 and root ten.

The negative root is thirty-nine. Both original roots lift uniquely, so the complete list is ten and thirty-nine.


A fraction modulo a prime means multiplication by an inverse. Its denominator must be nonzero modulo that prime. Record excluded values before clearing denominators.

Example. Solve 1/(x−1)+1/(x+1)≡1(mod7)1/(x-1)+1/(x+1)\equiv1\pmod7.

Exclude x≡1,−1x\equiv1,-1. Multiplying by the invertible denominator gives 2x≡x2−12x\equiv x^2-1, or (x−1)2≡2(x-1)^2\equiv2. The square roots of two modulo seven are three and negative three, so x≡4x\equiv4 or five.

Neither is excluded, and substitution verifies both. At four, the inverses are five and three, whose sum is one modulo seven. The other candidate works the same way.

The polynomial obtained by clearing denominators may have a root at an excluded value in other problems. Solving it is only the candidate-generation step; the original modular domain must still be checked.


The coefficient 2x02x_0 in a square-root lifting equation must be invertible for uniqueness. If it vanishes modulo the prime, there may be several lifts or none.

For example, x2≡0(modp2)x^2\equiv0\pmod{p^2} is equivalent to p∣xp\mid x. Its solutions modulo p2p^2 are 0,p,2p,…,(p−1)p0,p,2p,\ldots,(p-1)p, giving pp different lifts of the single zero root modulo pp.

Thus “every root lifts uniquely” requires the nondegeneracy hypothesis. The expansion proves which linear congruence must be solved and makes the exceptional case visible rather than hiding it in a memorized theorem.