Skip to content

Fermat's Little Theorem and Orders

Updated Β· Equations updated; Sections updated; Examples or practice updated; Explanations expanded What changed?
  1. β€” Equations updated; Sections updated; Examples or practice updated; Explanations expanded

Remainder cycles become more predictable when the base is coprime to the modulus. For a prime pp and an integer aa not divisible by pp, Fermat’s little theorem states

apβˆ’1≑1(modp).a^{p-1}\equiv1\pmod p.

Multiplication by aa permutes the nonzero residues modulo pp. Multiplying all those residues gives apβˆ’1(pβˆ’1)!≑(pβˆ’1)!a^{p-1}(p-1)!\equiv(p-1)!; the factorial is invertible modulo pp, so it cancels.

The equivalent statement ap≑a(modp)a^p\equiv a\pmod p holds for every integer aa, including multiples of pp.

Example. Find the remainder when 31003^{100} is divided by 77.

Since 36≑1(mod7)3^6\equiv1\pmod7 and 100=6β‹…16+4100=6\cdot16+4,

3100≑34=81≑4(mod7).3^{100}\equiv3^4=81\equiv4\pmod7.

Do not replace pβˆ’1p-1 by mβˆ’1m-1 for an arbitrary composite modulus. A different exponent is needed.


The totient Ο†(m)\varphi(m) counts integers in {1,…,m}\{1,\ldots,m\} coprime to mm. If the distinct prime divisors of mm are pp, inclusion–exclusion gives

Ο†(m)=m∏p∣m(1βˆ’1p).\varphi(m)=m\prod_{p\mid m}\left(1-\frac1p\right).

For gcd⁑(a,m)=1\gcd(a,m)=1, the same residue-permutation proof gives Euler’s theorem:

aΟ†(m)≑1(modm).a^{\varphi(m)}\equiv1\pmod m.

Example. Find the last two decimal digits of 72227^{222}.

Since 100=22β‹…52100=2^2\cdot5^2, we have Ο†(100)=100(1βˆ’1/2)(1βˆ’1/5)=40\varphi(100)=100(1-1/2)(1-1/5)=40. Euler’s theorem permits reduction of the exponent modulo forty. In this case a shorter cycle is visible: 74=2401≑1(mod100)7^4=2401\equiv1\pmod{100}. Therefore

7222≑72≑49(mod100).7^{222}\equiv7^2\equiv49\pmod{100}.

The last two digits are 4949.

If the base shares a factor with the modulus, exponent reduction can fail. For example, 1≑5(modΟ†(8))1\equiv5\pmod{\varphi(8)}, but 21≑2(mod8)2^1\equiv2\pmod8 while 25≑0(mod8)2^5\equiv0\pmod8. Always check coprimality before using Euler’s theorem.


For gcd⁑(a,m)=1\gcd(a,m)=1, the order ord⁑m(a)\operatorname{ord}_m(a) is the smallest positive integer dd with ad≑1(modm)a^d\equiv1\pmod m. Every exponent giving one is a multiple of dd.

To prove the last statement, divide an exponent kk by dd: k=qd+rk=qd+r with 0≀r<d0\le r<d. If ak≑1a^k\equiv1, then ar≑1a^r\equiv1, so minimality forces r=0r=0. In particular,

ord⁑m(a)βˆ£Ο†(m).\operatorname{ord}_m(a)\mid\varphi(m).

Example. Find all positive integers nn for which 7∣2nβˆ’17\mid2^n-1.

The first three powers of two modulo seven are 2,4,12,4,1, so ord⁑7(2)=3\operatorname{ord}_7(2)=3. Hence 2n≑1(mod7)2^n\equiv1\pmod7 exactly when 3∣n3\mid n. The solutions are all positive multiples of three.

To find an order, test divisors of Ο†(m)\varphi(m) rather than every exponent. For a prime modulus pp, a number with order pβˆ’1p-1 is called a primitive root. For instance, two has order ten modulo eleven because 25β‰‘βˆ’12^5\equiv-1, 22≑̸12^2\not\equiv1, and 2≑̸12\not\equiv1; these exclude all proper divisors of ten.


If a prime pp divides an expression like anβˆ’1a^n-1, the order of aa modulo pp links the exponent nn to pβˆ’1p-1. For an+1a^n+1, use a2n≑1a^{2n}\equiv1 while keeping anβ‰‘βˆ’1a^n\equiv-1.

Example. Let an odd prime pp divide 216+12^{16}+1. Prove p≑1(mod32)p\equiv1\pmod{32}.

We have 216β‰‘βˆ’1(modp)2^{16}\equiv-1\pmod p and 232≑1(modp)2^{32}\equiv1\pmod p. The order divides 3232 but does not divide 1616. Every proper divisor of 3232 divides 1616, so the order is exactly 3232. Fermat’s theorem gives 32∣pβˆ’132\mid p-1.

The power-of-two exponent makes the order exact. For a general an+1a^n+1, the order need not equal 2n2n, so prove it rather than assuming it.


For a prime pp,

(pβˆ’1)!β‰‘βˆ’1(modp).(p-1)!\equiv-1\pmod p.

For odd pp, pair each nonzero residue with its multiplicative inverse. Only 11 and βˆ’1-1 are their own inverses, because x2≑1x^2\equiv1 implies (xβˆ’1)(x+1)≑0(x-1)(x+1)\equiv0 modulo a prime. All other pairs multiply to one, leaving product βˆ’1-1. The prime 22 can be checked directly.

Example. Find the remainder of 9!9! modulo 1111.

Wilson gives 10!β‰‘βˆ’1(mod11)10!\equiv-1\pmod{11}. Since 10β‰‘βˆ’110\equiv-1,

βˆ’9!β‰‘βˆ’1(mod11)⟹9!≑1(mod11).-9!\equiv-1\pmod{11}\quad\Longrightarrow\quad9!\equiv1\pmod{11}.

Once Euler or Fermat supplies an exponent MM with aM≑1(modm)a^M\equiv1\pmod m, the actual order divides MM. Factor MM and repeatedly test whether the exponent can be divided by one of its prime factors.

If aM/q≑1a^{M/q}\equiv1, replace MM by M/qM/q and continue. If the test fails, the order requires that remaining prime-power contribution. This is faster than checking all exponents and makes the minimality argument explicit.

Example. Show that three has order sixteen modulo seventeen.

Fermat gives an order dividing sixteen. Compute

32≑9,34≑13,38≑16β‰‘βˆ’1(mod17).3^2\equiv9,\qquad3^4\equiv13,\qquad3^8\equiv16\equiv-1\pmod{17}.

The order does not divide eight. Every proper divisor of sixteen does divide eight, so the order must be sixteen.

When the candidate exponent has several prime factors, failing only one reduced-exponent test may not establish the full order. To prove order twelve, for example, check that the twelfth power is one while the sixth and fourth powers are not. Every proper divisor of twelve divides at least one of six or four.


For coprime moduli u,vu,v and a base coprime to both,

ord⁑uv(a)=lcm⁑(ord⁑u(a),ord⁑v(a)).\operatorname{ord}_{uv}(a) =\operatorname{lcm}(\operatorname{ord}_u(a),\operatorname{ord}_v(a)).

An exponent produces one modulo the product exactly when it produces one modulo both factors. The two order divisibility requirements are therefore equivalent to being a multiple of their least common multiple.

Example. Find the smallest positive nn for which 63∣2nβˆ’163\mid2^n-1.

Factor 63=7β‹…963=7\cdot9. The order of two modulo seven is three. Modulo nine, 23β‰‘βˆ’12^3\equiv-1 and 22≑̸12^2\not\equiv1, so the order is six.

Their least common multiple is six, and 26βˆ’1=632^6-1=63 confirms the answer.

This method also handles a simultaneous system of exponential divisibility conditions. First identify the order required by each modulus; only then combine the exponent conditions. Adding the orders or multiplying them indiscriminately can give a nonminimal answer.


For a power tower, reduce the outer exponent modulo the order of the outer base. The relevant modulus for the exponent is an order, not necessarily the original numerical modulus.

Example. Find the last two digits of 7777^{7^7}.

Modulo one hundred, the order of seven is four: 74≑17^4\equiv1 while 72≑49β‰ 17^2\equiv49\ne1. Thus only 77β€Šmodβ€Š47^7\bmod4 is needed.

Since 7β‰‘βˆ’1(mod4)7\equiv-1\pmod4 and seven is odd, 77≑3(mod4)7^7\equiv3\pmod4. Therefore

777≑73≑43(mod100).7^{7^7}\equiv7^3\equiv43\pmod{100}.

The final two digits are 43. Reducing the exponent modulo one hundred would not be the principle that justifies this calculation.

For a noncoprime base, a tower requires separate prime-power analysis. One part of the modulus may force a sufficiently high power to vanish, while another part cycles. Check this before building an exponent-reduction chain.


An order modulo pp does not automatically stay the same modulo p2p^2. Let pp be odd, p∀ap\nmid a, and d=ord⁑p(a)d=\operatorname{ord}_p(a). Write c=vp(adβˆ’1)c=v_p(a^d-1). For kβ‰₯1k\ge1,

ord⁑pk(a)=d pmax⁑(0,kβˆ’c).\operatorname{ord}_{p^k}(a)=d\,p^{\max(0,k-c)}.

To derive the formula, any successful exponent is dtdt because reduction modulo pp already forces that multiple. LTE gives

vp(adtβˆ’1)=vp(adβˆ’1)+vp(t)=c+vp(t).v_p(a^{dt}-1)=v_p(a^d-1)+v_p(t)=c+v_p(t).

Divisibility by pkp^k therefore requires exactly the extra power pmax⁑(0,kβˆ’c)p^{\max(0,k-c)} in tt. This links order, divisibility, and valuation in one calculation.

Example. Find the orders of two modulo forty-nine and three hundred forty-three.

Modulo seven, two has order three, and 23βˆ’1=72^3-1=7 has valuation one. The formula gives

ord⁑49(2)=3β‹…7=21,ord⁑343(2)=3β‹…72=147.\operatorname{ord}_{49}(2)=3\cdot7=21,\qquad \operatorname{ord}_{343}(2)=3\cdot7^2=147.

The prime-power formula used the odd-prime LTE hypotheses. Powers of two have different lifting behavior and must be handled separately.


A primitive root modulo an odd prime generates every nonzero residue by its powers. If gg has order pβˆ’1p-1, the equality gu≑gv(modp)g^u\equiv g^v\pmod p is equivalent to u≑v(modpβˆ’1)u\equiv v\pmod{p-1}.

Thus an equation between powers of the same primitive root becomes a linear congruence in the exponents. If the base has smaller order, use that smaller order instead.

Example. Find all positive integers nn satisfying 33n+2β‰‘βˆ’1(mod17)3^{3n+2}\equiv-1\pmod{17}.

We proved three has order sixteen. Since 38β‰‘βˆ’13^8\equiv-1, the condition is

3n+2≑8(mod16).3n+2\equiv8\pmod{16}.

The inverse of three modulo sixteen is eleven, giving n≑2(mod16)n\equiv2\pmod{16}. All positive integers in this class work, and order sixteen proves no other classes do.

An odd prime admits primitive roots, but finding a convenient one is a separate task. For many contest problems, determining the order of the specific given base is enough; constructing a generator for the whole residue group would add unnecessary work.


If pp is prime, every base coprime to it satisfies apβˆ’1≑1a^{p-1}\equiv1. The converse is false. Some composite numbers satisfy the same congruence for every coprime base; these are Carmichael numbers.

For example, 561=3β‹…11β‹…17561=3\cdot11\cdot17. If aa is coprime to 561, Fermat applied to the three prime factors gives exponents two, ten, and sixteen. Each divides 560, so a560≑1a^{560}\equiv1 modulo each factor, and CRT gives

a560≑1(mod561).a^{560}\equiv1\pmod{561}.

Thus passing a Fermat congruence does not establish primality. The argument does not require testing hundreds of bases; the prime-factor structure explains every coprime base at once.

Wilson’s theorem has a genuine converse for integers greater than one, but computing the factorial may be impractical. Distinguish a theorem’s logical strength from the efficiency of using it in a numerical problem.


If gcd⁑(m,10)=1\gcd(m,10)=1, the repeating decimal period of 1/m1/m is ord⁑m(10)\operatorname{ord}_m(10). Long division returns to its initial remainder precisely when 10d≑1(modm)10^d\equiv1\pmod m. For denominator seven, the order is six, explaining the six-digit repeating block.

When the denominator contains powers of two or five, those factors produce an initial nonrepeating portion. The period afterward is controlled by the remaining factor coprime to ten.

Wilson also constructs a square root of negative one modulo a prime p≑1(mod4)p\equiv1\pmod4. Set h=(pβˆ’1)/2h=(p-1)/2 and pair the factorial terms:

(pβˆ’1)!≑(βˆ’1)h(h!)2(modp).(p-1)!\equiv(-1)^h(h!)^2\pmod p.

Here hh is even, so Wilson gives (h!)2β‰‘βˆ’1(modp)(h!)^2\equiv-1\pmod p. For p=13p=13, 6!≑56!\equiv5 and 52β‰‘βˆ’15^2\equiv-1. This provides an explicit root and connects factorial identities with quadratic residues.


Define Fn=22n+1F_n=2^{2^n}+1. Repeated difference-of-squares factorization gives

F0F1β‹―Fnβˆ’1=Fnβˆ’2.F_0F_1\cdots F_{n-1}=F_n-2.

One proof is induction: multiplying 22nβˆ’12^{2^n}-1 by 22n+12^{2^n}+1 produces 22n+1βˆ’12^{2^{n+1}}-1, the next required product.

Any common divisor of FnF_n and an earlier FiF_i therefore divides two. All Fermat numbers are odd, so they are pairwise coprime. Choosing a prime divisor from each gives infinitely many distinct primes.

If an odd prime pp divides FnF_n, then 22nβ‰‘βˆ’1(modp)2^{2^n}\equiv-1\pmod p. Its order is exactly 2n+12^{n+1}, since every proper divisor of that power of two divides 2n2^n. Therefore p≑1(mod2n+1)p\equiv1\pmod{2^{n+1}}.

The numbers themselves need not be prime. For example, 641 divides F5=232+1F_5=2^{32}+1. To verify this without long division, use

641=5β‹…27+1=54+24.641=5\cdot2^7+1=5^4+2^4.

The first identity gives 54228≑15^4 2^{28}\equiv1 modulo 641, while the second gives 54β‰‘βˆ’245^4\equiv-2^4. Hence βˆ’232≑1-2^{32}\equiv1, proving the divisibility.

Pairwise coprimality guarantees new prime factors, not primality of every term. That distinction is the point of the construction.


If gg is primitive modulo an odd prime pp, every nonzero residue is uniquely gtg^t with tt taken modulo pβˆ’1p-1. Thus xk≑1(modp)x^k\equiv1\pmod p becomes kt≑0(modpβˆ’1)kt\equiv0\pmod{p-1} and has exactly gcd⁑(k,pβˆ’1)\gcd(k,p-1) solutions.

Example. Find every solution of x4≑1(mod17)x^4\equiv1\pmod{17}.

There are exactly four solutions because gcd⁑(4,16)=4\gcd(4,16)=4. Using primitive root three, the allowed exponents are t=0,4,8,12t=0,4,8,12, giving

x≑1,13,16,4(mod17).x\equiv1,13,16,4\pmod{17}.

The factorization (x2βˆ’1)(x2+1)(x^2-1)(x^2+1) provides a second route: the roots of one are positive and negative one, and the roots of negative one are four and thirteen.

The count follows from a linear congruence in exponents, while the polynomial degree confirms there cannot be more than four roots over the prime field.


For an integer n>1n>1, if (nβˆ’1)!β‰‘βˆ’1(modn)(n-1)!\equiv-1\pmod n, then nn is prime. If it were composite, it would have a divisor dd with 1<d<n1<d<n. That divisor occurs in the factorial, so reducing the congruence modulo dd would say zero is negative one, a contradiction.

Thus Wilson provides an if-and-only-if primality criterion, unlike Fermat. Its drawback is computational rather than logical: a factorial near a large candidate can be costly to evaluate. In contest problems, Wilson is most effective when almost all factors cancel or pair with inverses.


If aa has order dd modulo mm, then

ord⁑m(ak)=dgcd⁑(d,k)\operatorname{ord}_m(a^k)=\frac d{\gcd(d,k)}

for positive kk. Indeed, (ak)t(a^k)^t equals one precisely when d∣ktd\mid kt, whose least positive solution is the displayed quotient.

For example, three has order sixteen modulo seventeen. Its square nine has order eight, while its cube twenty-seven still has order sixteen because three is coprime to sixteen.

This rule is useful when several bases in a problem are powers of one known element. It avoids recalculating a full remainder cycle for each base and explains exactly when taking a power shortens that cycle.