Modular Exponentiation and the Euler-Fermat Theorem

How can a computer compute 71000000  917^{1000000}\bmod 91? It must do this without ever writing a million-digit number. This practical question forces us to confront something beautiful. Modular arithmetic hides a piece of elegant structure inside it. The multiplicative group of integers modulo nn is finite. So every element raised to a high enough power must cycle back. It returns to where it started. Euler's theorem makes this precise. It shows that aϕ(n)≡1(modn)a^{\phi(n)} \equiv 1 \pmod n whenever aa and nn are coprime. Here ϕ\phi is The Euler Totient Function. This single congruence is the engine behind an entire computational and cryptographic industry. The idea matters for a key reason. It converts a statement about counting into a statement about periodicity. Counting means asking how many integers below nn are coprime to nn. Periodicity means asking how quickly powers of aa return to 11. That bridge — from counting to cycling — is one of the recurring miracles of elementary number theory. It generalizes the older, more restrictive Fermat's Little Theorem from prime moduli to arbitrary moduli. The big picture is this: (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times, the set of residues coprime to nn under multiplication, is a finite abelian group of order ϕ(n)\phi(n). Euler's theorem is nothing more than Lagrange's theorem in disguise. It is also a direct consequence of a clever rearrangement argument. That argument predates group theory itself. This article builds the theorem from first principles. It uses only Modular Arithmetic and the definition of ϕ\phi. It gives a complete, self-contained proof by the classical "permutation of residues" method. This is the same style of argument Euler himself would have recognized. We then turn to the algorithmic payoff. Fast modular exponentiation works by repeated squaring. This lets us compute ak  na^k \bmod n in about log⁡2k\log_2 k multiplications instead of kk multiplications. Each multiplication stays small by reducing modulo nn at every step. The article closes by pointing toward where this machinery becomes indispensable. That place is the RSA Cryptosystem. Its security and correctness rest entirely on Euler's theorem. This theorem applies to a modulus n=pqn = pq, a product of two large primes. Everything proved here — the theorem and the fast exponentiation algorithm — is exactly the toolkit that public-key cryptography needs.

Key takeaways
  • Euler's theorem: if gcd⁡(a,n)=1\gcd(a,n)=1 then aϕ(n)≡1(modn)a^{\phi(n)} \equiv 1 \pmod n, generalizing Fermat's Little Theorem.
  • Proof strategy: multiplication by aa permutes the residues coprime to nn; multiply them all and cancel.
  • Fermat's Little Theorem is the special case n=pn=p prime, where ϕ(p)=p−1\phi(p) = p-1.
  • Fast modular exponentiation (repeated squaring) computes ak  na^k \bmod n in O(log⁡k)O(\log k) modular multiplications.
  • The order of aa modulo nn always divides ϕ(n)\phi(n) — a consequence of Lagrange's theorem for the group (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times.
  • This pair of tools (theorem + algorithm) is the exact computational backbone of RSA encryption and decryption.
  • Key idea: finiteness of the coprime residues forces every coprime power sequence to be eventually periodic with period dividing ϕ(n)\phi(n).

Where this lives today

Euler's theorem and fast modular exponentiation together form the computational core of modern public-key cryptography. In the RSA Cryptosystem, a modulus n=pqn = pq is chosen as a product of two large primes, and encryption/decryption exponents e,de, d are chosen so that ed≡1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}; correctness of decryption is a direct application of Euler's theorem (with some extra care for messages not coprime to nn, handled via the Chinese Remainder Theorem). Without fast modular exponentiation, computing cd  nc^d \bmod n for a 2048-bit exponent dd would be entirely infeasible; repeated squaring makes it routine, requiring only a few thousand modular multiplications rather than an astronomically large number. Beyond RSA, the same machinery underlies the Diffie–Hellman key exchange, Rabin cryptosystem, and various digital signature schemes, all of which perform exponentiation in a finite group of known or partially known order. In primality testing, Fermat's Little Theorem (the prime-modulus special case) is the seed idea behind the Fermat primality test and its refinements (Miller–Rabin), which rely on fast modular exponentiation to test candidate primes with hundreds of digits in milliseconds. In computer algebra and coding theory, modular exponentiation appears in polynomial evaluation over finite fields, and the order-divides-group-order corollary generalizes to the structure theory of finite fields and cyclic groups used in error-correcting codes. Even outside cryptography, Euler's theorem is a standard tool in competition mathematics and in analytic number theory for controlling the multiplicative order of residues, feeding into deeper results about the distribution of primes and the structure of (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times.

Ask the tutor: How does Euler's theorem, combined with the Chinese Remainder Theorem, guarantee that RSA decryption recovers the original message even when the message shares a factor with the modulus?

DefinitionEuler's totient function, coprime residues, and the statement to be proved

Let n≥1n \geq 1 be an integer. Define

ϕ(n)=#{k∈{1,2,…,n}:gcd⁡(k,n)=1}.\phi(n) = \#\{ k \in \{1, 2, \dots, n\} : \gcd(k,n) = 1 \}.

Let (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times denote the set of residue classes modulo nn that have a representative coprime to nn; this set has exactly ϕ(n)\phi(n) elements, and it is closed under multiplication modulo nn (if gcd⁡(a,n)=gcd⁡(b,n)=1\gcd(a,n)=\gcd(b,n)=1 then gcd⁡(ab,n)=1\gcd(ab,n)=1). For integers aa and nn with n≥1n \geq 1 and gcd⁡(a,n)=1\gcd(a,n) = 1, Euler's theorem asserts

aϕ(n)≡1(modn).a^{\phi(n)} \equiv 1 \pmod n.

The order of aa modulo nn, written ordn(a)\mathrm{ord}_n(a), is the smallest positive integer dd such that ad≡1(modn)a^d \equiv 1 \pmod n; Euler's theorem guarantees this order exists (since ϕ(n)\phi(n) itself is one such exponent), and a companion fact proved below shows ordn(a)∣ϕ(n)\mathrm{ord}_n(a) \mid \phi(n).

Intuition

Think of the coprime residues modulo nn as points arranged in a cycle, and think of multiplication by aa as a shuffle of these points. Because aa is invertible modulo nn (this is exactly what gcd⁡(a,n)=1\gcd(a,n)=1 buys us), the shuffle is a bijection: no two points collide, and no point is left out. A finite set shuffled bijectively by repeatedly applying the same rule must eventually return every point to its original position — you cannot shuffle a finite deck forever without repeating a configuration. Euler's theorem pins down exactly how many shuffles it takes in the worst case: ϕ(n)\phi(n) applications of "multiply by aa" always return every coprime residue to itself, though the true minimal period (the order of aa) may be a proper divisor of ϕ(n)\phi(n). A second way to see it: the coprime residues modulo nn form a group under multiplication, of size ϕ(n)\phi(n). The powers of aa trace out a subgroup — a smaller cycle sitting inside the big cycle. Lagrange's theorem, a purely group-theoretic fact, says the size of any subgroup divides the size of the whole group. Since the size of the subgroup generated by aa is precisely the order of aa, this order must divide ϕ(n)\phi(n), and raising aa to the ϕ(n)\phi(n)-th power is just going around the big cycle a whole number of times, landing back at the identity element 11.

Figure
The multiplicative group modulo 9 as a cycle: powers of 2 return to 1 after order 6, which divides φ(9)=6
Residues coprime to 9: {1,2,4,5,7,8}, φ(9)=6 1 2 4 8 7 5 2^6 = 64 ≡ 1 (mod 9): order 6 divides φ(9) = 6

The central theorem

TheoremEuler's Theorem

Let nn be a positive integer and let aa be an integer with gcd⁡(a,n)=1\gcd(a, n) = 1. Then

aϕ(n)≡1(modn).a^{\phi(n)} \equiv 1 \pmod n.
LemmaMultiplication by aa permutes the coprime residues

Let n≥1n \geq 1 and let aa be an integer with gcd⁡(a,n)=1\gcd(a,n) = 1. Let r1,r2,…,rϕ(n)r_1, r_2, \dots, r_{\phi(n)} be the distinct residues modulo nn in {1,…,n}\{1, \dots, n\} that are coprime to nn (there are ϕ(n)\phi(n) of them by definition of the totient). Then the residues

ar1  n,ar2  n,…,arϕ(n)  na r_1 \bmod n, \quad a r_2 \bmod n, \quad \dots, \quad a r_{\phi(n)} \bmod n

are again exactly the numbers r1,…,rϕ(n)r_1, \dots, r_{\phi(n)}, in some order.

Proof

We use the technique of exhibiting a bijection between finite sets by showing the map is injective (hence, by finiteness, a permutation), and checking the outputs land in the same set as the inputs. Step 1: each ari  nar_i \bmod n is coprime to nn. Since gcd⁡(a,n)=1\gcd(a,n)=1 and gcd⁡(ri,n)=1\gcd(r_i, n) = 1, and since gcd⁡(ari,n)=1\gcd(ar_i, n) = 1 whenever gcd⁡(a,n)=gcd⁡(ri,n)=1\gcd(a,n)=\gcd(r_i,n)=1 (a standard fact: if a prime p∣np \mid n divided ariar_i, then since pp is prime it would divide aa or rir_i, contradicting one of the two coprimality hypotheses), we get gcd⁡(ari,n)=1\gcd(a r_i, n) = 1. Reducing modulo nn does not change this gcd, since ari≡(ari  n)(modn)ar_i \equiv (ar_i \bmod n) \pmod n and gcd with nn is a function of the residue class alone (if x≡y(modn)x \equiv y \pmod n then x=y+knx = y + kn for some integer kk, so any common divisor of nn and yy also divides xx, and vice versa). Hence ari  nar_i \bmod n is one of the ϕ(n)\phi(n) numbers in {1,…,n}\{1,\dots,n\} coprime to nn, i.e., it is one of r1,…,rϕ(n)r_1,\dots,r_{\phi(n)}. Step 2: the map ri↦ari  nr_i \mapsto ar_i \bmod n is injective. Suppose ari≡arj(modn)a r_i \equiv a r_j \pmod n for some i,ji, j. Then n∣a(ri−rj)n \mid a(r_i - r_j). Since gcd⁡(a,n)=1\gcd(a, n) = 1, Euclid's lemma (in the form: if gcd⁡(a,n)=1\gcd(a,n)=1 and n∣amn \mid a m then n∣mn \mid m, which follows from Bézout's identity ua+vn=1ua + vn = 1 for integers u,vu,v, giving m=uam+vnmm = uam + vnm, and nn divides both terms on the right) implies n∣(ri−rj)n \mid (r_i - r_j). Since ri,rj∈{1,…,n}r_i, r_j \in \{1, \dots, n\}, we have ∣ri−rj∣<n|r_i - r_j| < n, so n∣(ri−rj)n \mid (r_i - r_j) forces ri=rjr_i = r_j, and hence i=ji = j (as the rir_i are listed as distinct residues). Step 3: conclude it is a bijection onto the same finite set. We have a map from the finite set {r1,…,rϕ(n)}\{r_1, \dots, r_{\phi(n)}\} to itself (Step 1) that is injective (Step 2). An injective map from a finite set to itself is automatically surjective (a basic pigeonhole fact: if it missed some element, the images of the ϕ(n)\phi(n) distinct inputs would occupy at most ϕ(n)−1\phi(n)-1 slots among the ϕ(n)\phi(n) outputs, forcing two inputs to share an output, contradicting injectivity). Hence the map ri↦ari  nr_i \mapsto ar_i \bmod n is a bijection of the set {r1,…,rϕ(n)}\{r_1, \dots, r_{\phi(n)}\} onto itself — that is, a permutation, exactly as claimed.

∎
Proof

(Proof of Euler's Theorem, using the Lemma.) We use the technique of multiplying all elements of a permuted finite set and cancelling the common factor — the classical argument due to Euler. By the Lemma, the numbers ar1  n,…,arϕ(n)  nar_1 \bmod n, \dots, ar_{\phi(n)} \bmod n are a rearrangement of r1,…,rϕ(n)r_1, \dots, r_{\phi(n)}. Therefore their products modulo nn agree:

∏i=1ϕ(n)(ari  n)≡∏i=1ϕ(n)ri(modn).\prod_{i=1}^{\phi(n)} (a r_i \bmod n) \equiv \prod_{i=1}^{\phi(n)} r_i \pmod n.

Since each ari  na r_i \bmod n is congruent to aria r_i modulo nn, and a product of numbers congruent term-by-term modulo nn is congruent modulo nn (an immediate induction on the number of factors, using that x≡x′,y≡y′(modn)⇒xy≡x′y′(modn)x \equiv x' , y\equiv y' \pmod n \Rightarrow xy \equiv x'y' \pmod n, itself immediate from xy−x′y′=x(y−y′)+y′(x−x′)xy - x'y' = x(y-y') + y'(x-x')), we may replace (ari  n)(ar_i \bmod n) by ariar_i throughout:

∏i=1ϕ(n)(ari)≡∏i=1ϕ(n)ri(modn).\prod_{i=1}^{\phi(n)} (a r_i) \equiv \prod_{i=1}^{\phi(n)} r_i \pmod n.

The left side factors as

aϕ(n)∏i=1ϕ(n)ri≡∏i=1ϕ(n)ri(modn).a^{\phi(n)} \prod_{i=1}^{\phi(n)} r_i \equiv \prod_{i=1}^{\phi(n)} r_i \pmod n.

Let R=∏i=1ϕ(n)riR = \prod_{i=1}^{\phi(n)} r_i. We have shown n∣aϕ(n)R−R=R(aϕ(n)−1)n \mid a^{\phi(n)} R - R = R\left(a^{\phi(n)} - 1\right). We now claim gcd⁡(R,n)=1\gcd(R, n) = 1. Each rir_i satisfies gcd⁡(ri,n)=1\gcd(r_i, n) = 1, and a product of integers each coprime to nn is itself coprime to nn (if a prime p∣np \mid n divided the product RR, then since pp is prime it would divide one of the factors rir_i, contradicting gcd⁡(ri,n)=1\gcd(r_i,n)=1). Hence gcd⁡(R,n)=1\gcd(R, n) = 1. Since n∣R(aϕ(n)−1)n \mid R(a^{\phi(n)} - 1) and gcd⁡(R,n)=1\gcd(R, n) = 1, Euclid's lemma (as invoked above, via Bézout) gives

n∣aϕ(n)−1,i.e.,aϕ(n)≡1(modn).n \mid a^{\phi(n)} - 1, \quad \text{i.e.,} \quad a^{\phi(n)} \equiv 1 \pmod n.

This completes the proof. ■\blacksquare

∎
CorollaryFermat's Little Theorem

If pp is a prime and aa is any integer with p∤ap \nmid a, then

ap−1≡1(modp).a^{p-1} \equiv 1 \pmod p.
Proof

For a prime pp, every one of the integers 1,2,…,p−11, 2, \dots, p-1 is coprime to pp (none of them share the factor pp, since pp is prime and each is smaller than pp and nonzero), while pp itself is not coprime to pp. Hence ϕ(p)=p−1\phi(p) = p - 1. The hypothesis p∤ap \nmid a is exactly gcd⁡(a,p)=1\gcd(a,p) = 1 (since the only divisors of the prime pp are 11 and pp). Applying Euler's Theorem with n=pn = p gives aϕ(p)=ap−1≡1(modp)a^{\phi(p)} = a^{p-1} \equiv 1 \pmod p. ■\blacksquare

∎
CorollaryThe order of aa divides ϕ(n)\phi(n)

If gcd⁡(a,n)=1\gcd(a,n) = 1, then ordn(a)\mathrm{ord}_n(a) exists and divides ϕ(n)\phi(n).

Proof

Existence: Euler's theorem exhibits ϕ(n)\phi(n) as a positive integer dd with ad≡1(modn)a^d \equiv 1 \pmod n, so the set of such positive exponents is nonempty; by well-ordering it has a least element, which is ordn(a)\mathrm{ord}_n(a) by definition. Call it d0d_0. Divisibility: apply the division algorithm to write ϕ(n)=q⋅d0+r\phi(n) = q \cdot d_0 + r with 0≤r<d00 \le r < d_0. Then

1≡aϕ(n)=(ad0)q⋅ar≡1q⋅ar=ar(modn),1 \equiv a^{\phi(n)} = \left(a^{d_0}\right)^q \cdot a^r \equiv 1^q \cdot a^r = a^r \pmod n,

using ad0≡1(modn)a^{d_0} \equiv 1 \pmod n from the definition of d0d_0. So ar≡1(modn)a^r \equiv 1 \pmod n with 0≤r<d00 \le r < d_0. Since d0d_0 is the smallest positive exponent with this property, rr cannot be a positive integer less than d0d_0 satisfying ar≡1a^r \equiv 1; hence r=0r = 0. Thus ϕ(n)=q⋅d0\phi(n) = q \cdot d_0, i.e., d0∣ϕ(n)d_0 \mid \phi(n). ■\blacksquare

∎

Fast modular exponentiation

Euler's theorem tells us aϕ(n)≡1(modn)a^{\phi(n)} \equiv 1 \pmod n, but in applications (and especially in cryptography) we need to compute ak  na^k \bmod n for specific, often astronomically large, exponents kk — we cannot simply reduce kk modulo ϕ(n)\phi(n) unless we already know ϕ(n)\phi(n), and even then we must still exponentiate efficiently. The naive approach of multiplying aa by itself k−1k-1 times is computationally infeasible once kk has, say, hundreds of digits. The algorithm of repeated squaring (also called binary exponentiation, or exponentiation by squaring) solves this by exploiting the binary representation of kk.

TheoremCorrectness and complexity of fast modular exponentiation

Let a,n,ka, n, k be integers with n≥1n \geq 1 and k≥0k \geq 0. Write the binary expansion of kk as

k=∑i=0mbi2i,bi∈{0,1}, bm=1 if k>0.k = \sum_{i=0}^{m} b_i 2^i, \qquad b_i \in \{0,1\}, \ b_m = 1 \text{ if } k > 0.

Define the sequence S0=a  nS_0 = a \bmod n, and Si+1=Si2  nS_{i+1} = S_i^2 \bmod n for i=0,…,m−1i = 0, \dots, m-1 (so Si≡a2i(modn)S_i \equiv a^{2^i} \pmod n). Then

ak  n=(∏i:bi=1Si)  n,a^k \bmod n = \left( \prod_{i : b_i = 1} S_i \right) \bmod n,

and this value can be computed using at most 2⌊log⁡2k⌋2\lfloor \log_2 k \rfloor modular multiplications (each performed on numbers already reduced modulo nn), i.e. O(log⁡k)O(\log k) multiplications, as opposed to k−1k-1 multiplications for naive repeated multiplication.

Proof

(Proof by induction on ii, then assembly of the binary sum — the technique of "repeated squaring".) Claim: Si≡a2i(modn)S_i \equiv a^{2^i} \pmod n for all i≥0i \ge 0. We induct on ii. Base case i=0i=0: S0=a  n≡a=a20(modn)S_0 = a \bmod n \equiv a = a^{2^0} \pmod n. Inductive step: suppose Si≡a2i(modn)S_i \equiv a^{2^i} \pmod n. Since congruences are preserved under multiplication (as used earlier), squaring both sides gives Si2≡(a2i)2=a2i+1(modn)S_i^2 \equiv \left(a^{2^i}\right)^2 = a^{2^{i+1}} \pmod n. Reducing modulo nn does not change the residue class, so Si+1=Si2  n≡Si2≡a2i+1(modn)S_{i+1} = S_i^2 \bmod n \equiv S_i^2 \equiv a^{2^{i+1}} \pmod n. This completes the induction. Assembling aka^k. By the binary expansion, k=∑i=0mbi2ik = \sum_{i=0}^m b_i 2^i, so by the laws of exponents,

ak=a∑ibi2i=∏i=0mabi2i=∏i:bi=1a2i.a^k = a^{\sum_i b_i 2^i} = \prod_{i=0}^m a^{b_i 2^i} = \prod_{i : b_i = 1} a^{2^i}.

(Terms with bi=0b_i = 0 contribute a0=1a^0 = 1 and drop out.) Using the Claim, a2i≡Si(modn)a^{2^i} \equiv S_i \pmod n for each ii appearing in the product, and since a product of congruent terms is congruent (as before), we get

ak≡∏i:bi=1Si(modn).a^k \equiv \prod_{i : b_i = 1} S_i \pmod n.

Reducing the right side modulo nn at the end (or, equivalently, at each multiplication step, which yields the same residue class by the same congruence-preservation fact) gives ak  n=(∏i:bi=1Si)  na^k \bmod n = \left(\prod_{i:b_i=1} S_i\right) \bmod n, as claimed. This proves correctness. Complexity count. Computing S0,S1,…,SmS_0, S_1, \dots, S_m requires mm squarings (one modular multiplication each), where m=⌊log⁡2k⌋m = \lfloor \log_2 k \rfloor is the position of the leading bit of kk (since 2m≤k<2m+12^m \le k < 2^{m+1}). Forming the product ∏i:bi=1Si\prod_{i: b_i=1} S_i requires at most mm further modular multiplications (one for each additional term beyond the first factor included). Hence the total number of modular multiplications is at most m+m=2⌊log⁡2k⌋m + m = 2\lfloor \log_2 k \rfloor, which is O(log⁡k)O(\log k). Crucially, every multiplication is immediately followed by a reduction modulo nn (in computing each SiS_i and each partial product), so every number ever multiplied is already less than nn in absolute value; the multiplicands never grow beyond roughly n2n^2 before reduction, regardless of how large kk is. This is what makes the algorithm practical even when kk has hundreds or thousands of digits. ■\blacksquare

∎

Worked examples

Example 1 (using Euler's theorem to reduce a large exponent, non-prime modulus). Compute 71000  247^{1000} \bmod 24. First, gcd⁡(7,24)=1\gcd(7, 24) = 1 since 24=23⋅324 = 2^3 \cdot 3 and 77 is divisible by neither 22 nor 33. Compute ϕ(24)\phi(24): using the standard product formula (a consequence of the multiplicativity of ϕ\phi, itself provable from the definition, though we take it as known here per the prerequisites) ϕ(24)=24(1−12)(1−13)=24⋅12⋅23=8\phi(24) = 24\left(1 - \tfrac12\right)\left(1 - \tfrac13\right) = 24 \cdot \tfrac12 \cdot \tfrac23 = 8. By Euler's theorem, 78≡1(mod24)7^8 \equiv 1 \pmod{24}. Now reduce the exponent: write 1000=8⋅125+01000 = 8 \cdot 125 + 0, so 1000=8⋅1251000 = 8\cdot 125 exactly, with remainder r=0r=0. Then

71000=(78)125≡1125=1(mod24).7^{1000} = \left(7^{8}\right)^{125} \equiv 1^{125} = 1 \pmod{24}.

Let's double check independently by directly computing small powers of 77 mod 2424: 71=77^1 = 7, 72=49≡1(mod24)7^2 = 49 \equiv 1 \pmod{24} (since 49=2⋅24+149 = 2\cdot 24 + 1). So actually the true order of 77 modulo 2424 is 22, a proper divisor of ϕ(24)=8\phi(24) = 8, consistent with the Corollary above (ord24(7)∣ϕ(24)\mathrm{ord}_{24}(7) \mid \phi(24), and indeed 2∣82 \mid 8). Since 10001000 is even, 71000=(72)500≡1500=1(mod24)7^{1000} = (7^2)^{500} \equiv 1^{500} = 1 \pmod{24}, confirming the answer 71000≡1(mod24)7^{1000} \equiv 1 \pmod{24}. This illustrates an important subtlety: Euler's theorem always gives a valid exponent (ϕ(n)\phi(n)) that works, but the true period (the order) can be much smaller, and knowing the order lets us reduce even faster — though Euler's theorem alone, without knowing the order, already suffices to get the correct final answer via the division 1000=8⋅1251000 = 8\cdot 125.

Example 2 (fast modular exponentiation by hand, asymmetric numbers). Compute ak  n=5117  91a^k \bmod n = 5^{117} \bmod 91 using repeated squaring, and cross-check with Euler's theorem. First, 91=7×1391 = 7 \times 13, so ϕ(91)=ϕ(7)ϕ(13)=6×12=72\phi(91) = \phi(7)\phi(13) = 6 \times 12 = 72 (using multiplicativity of ϕ\phi on the coprime factors 7,137, 13). Since gcd⁡(5,91)=1\gcd(5, 91) = 1 (as 55 shares no factor with 77 or 1313), Euler's theorem gives 572≡1(mod91)5^{72} \equiv 1 \pmod{91}. Binary expansion of the exponent. 117=64+32+16+4+1=26+25+24+22+20117 = 64 + 32 + 16 + 4 + 1 = 2^6 + 2^5 + 2^4 + 2^2 + 2^0. Check: 64+32+16+4+1=11764+32+16+4+1 = 117. ✓. So in binary, 117=11101012117 = 1110101_2, i.e. bits b0=1,b1=0,b2=1,b3=0,b4=1,b5=1,b6=1b_0=1, b_1=0, b_2=1, b_3=0, b_4=1,b_5=1,b_6=1. Building the squaring tower Si≡52i(mod91)S_i \equiv 5^{2^i} \pmod {91}: S0=5  91=5S_0 = 5 \bmod 91 = 5. S1=S02  91=25  91=25S_1 = S_0^2 \bmod 91 = 25 \bmod 91 = 25. S2=S12  91=625  91S_2 = S_1^2 \bmod 91 = 625 \bmod 91. Since 91×6=54691 \times 6 = 546 and 625−546=79625 - 546 = 79, S2=79S_2 = 79. S3=S22  91=792  91=6241  91S_3 = S_2^2 \bmod 91 = 79^2 \bmod 91 = 6241 \bmod 91. Since 91×68=618891 \times 68 = 6188, 6241−6188=536241 - 6188 = 53, so S3=53S_3 = 53. S4=S32  91=532  91=2809  91S_4 = S_3^2 \bmod 91 = 53^2 \bmod 91 = 2809 \bmod 91. Since 91×30=273091 \times 30 = 2730, 2809−2730=792809 - 2730 = 79, so S4=79S_4 = 79. (Sanity check on the cycle: S2=79S_2 = 79 and S4=79S_4 = 79 — this recurrence is plausible since 55 has some order dividing 7272, and squaring is periodic; we don't need this observation for the computation, we just continue mechanically.) S5=S42  91=792  91=6241  91=53S_5 = S_4^2 \bmod 91 = 79^2 \bmod 91 = 6241 \bmod 91 = 53 (identical computation to S3S_3, since S4=S2=79S_4 = S_2 = 79). S6=S52  91=532  91=2809  91=79S_6 = S_5^2 \bmod 91 = 53^2 \bmod 91 = 2809 \bmod 91 = 79 (identical computation to S4S_4). So the tower is:

S0=5, S1=25, S2=79, S3=53, S4=79, S5=53, S6=79.S_0=5,\ S_1=25,\ S_2=79,\ S_3=53,\ S_4=79,\ S_5=53,\ S_6=79.

Assembling the answer. We need ∏i:bi=1Si\prod_{i : b_i=1} S_i for i∈{0,2,4,5,6}i \in \{0,2,4,5,6\} (bits equal to 11 at positions 0,2,4,5,60,2,4,5,6, matching 20+22+24+25+26=1+4+16+32+64=1172^0+2^2+2^4+2^5+2^6 = 1+4+16+32+64=117 ✓). So

5117≡S0⋅S2⋅S4⋅S5⋅S6(mod91)=5⋅79⋅79⋅53⋅79(mod91).5^{117} \equiv S_0 \cdot S_2 \cdot S_4 \cdot S_5 \cdot S_6 \pmod{91} = 5 \cdot 79 \cdot 79 \cdot 53 \cdot 79 \pmod{91}.

Compute step by step, reducing modulo 9191 after each multiplication: 5×79=3955 \times 79 = 395. 395  91395 \bmod 91: 91×4=36491 \times 4 = 364, 395−364=31395 - 364 = 31. Running product: 3131. 31×79=244931 \times 79 = 2449. 2449  912449 \bmod 91: 91×26=236691 \times 26 = 2366, 2449−2366=832449 - 2366 = 83. Running product: 8383. 83×53=439983 \times 53 = 4399. 4399  914399 \bmod 91: 91×48=436891 \times 48 = 4368, 4399−4368=314399 - 4368 = 31. Running product: 3131. 31×79=2449≡83(mod91)31 \times 79 = 2449 \equiv 83 \pmod{91} (computed above). Running product: 8383. So 5117≡83(mod91)5^{117} \equiv 83 \pmod{91}. Cross-check via Euler's theorem. We know 572≡1(mod91)5^{72} \equiv 1 \pmod {91}. Write 117=72+45117 = 72 + 45, so 5117=572⋅545≡545(mod91)5^{117} = 5^{72} \cdot 5^{45} \equiv 5^{45} \pmod{91}. We can verify 545≡835^{45} \equiv 83 independently: note 45=32+8+4+1=25+23+22+2045 = 32+8+4+1 = 2^5+2^3+2^2+2^0, using bits at 0,2,3,50,2,3,5. We already have S0=5,S2=79,S5=53S_0=5, S_2=79, S_5=53; we need S3=53S_3 = 53 (already computed above). So

545≡S0⋅S2⋅S3⋅S5=5⋅79⋅53⋅53(mod91).5^{45} \equiv S_0 \cdot S_2 \cdot S_3 \cdot S_5 = 5 \cdot 79 \cdot 53 \cdot 53 \pmod{91}.

5×79=395≡315\times 79 = 395 \equiv 31 (as before). 31×53=164331 \times 53 = 1643; 91×18=163891\times 18 = 1638, remainder 55. Running product: 55. 5×53=2655 \times 53 = 265; 91×2=18291 \times 2 = 182, 265−182=83265-182=83. Running product: 8383. Both routes give 5117≡83(mod91)5^{117} \equiv 83 \pmod{91}, confirming the computation and simultaneously illustrating how Euler's theorem can be combined with repeated squaring to shrink the exponent before squaring, cutting the work further.

Counterexamplewhy coprimality of aa and nn cannot be dropped

Consider n=10n = 10, so ϕ(10)=ϕ(2)ϕ(5)=1×4=4\phi(10) = \phi(2)\phi(5) = 1 \times 4 = 4. Take a=5a = 5, which is not coprime to 1010 (indeed gcd⁡(5,10)=5≠1\gcd(5,10) = 5 \neq 1). If Euler's theorem held without the coprimality hypothesis, we would expect

5ϕ(10)=54≡1(mod10).5^{\phi(10)} = 5^4 \equiv 1 \pmod{10}.

But 54=6255^4 = 625, and 625=62×10+5625 = 62\times 10 + 5, so 54≡5(mod10)5^4 \equiv 5 \pmod{10}, not 11. In fact 5k≡5(mod10)5^k \equiv 5 \pmod{10} for every k≥1k \geq 1 (an easy induction: 52=25≡55^2 = 25 \equiv 5, and if 5k≡55^k \equiv 5 then 5k+1=5⋅5k≡5⋅5=25≡55^{k+1} = 5\cdot 5^k \equiv 5\cdot 5 = 25\equiv 5), so 5k5^k is never congruent to 1  101 \bmod 10 for any positive exponent kk — the sequence of powers of 55 mod 1010 is eventually constant at 55, never reaching 11. This shows the hypothesis gcd⁡(a,n)=1\gcd(a,n)=1 is not a technical nicety but is essential: without it, aa need not even lie in the multiplicative structure where "returning to 11" makes sense, since aa may not be invertible modulo nn at all (an element aa has a multiplicative inverse mod nn exactly when gcd⁡(a,n)=1\gcd(a,n)=1, by Bézout's identity), and the permutation argument in the Lemma breaks down completely: multiplication by a non-invertible aa is not injective on residues mod nn, so it cannot permute any set of representatives.

Scope, edge cases & common traps

SituationWhat happensCorrect handling
gcd⁡(a,n)=1\gcd(a,n) = 1, nn arbitraryEuler's theorem applies directlyaϕ(n)≡1(modn)a^{\phi(n)} \equiv 1 \pmod n
gcd⁡(a,n)≠1\gcd(a,n) \neq 1Theorem does not apply; conclusion can failFactor out common divisors first, or use other tools (e.g. CRT-style case analysis)
nn primeEuler's theorem reduces to Fermat's Little Theoremϕ(p)=p−1\phi(p) = p-1
Exponent kk larger than ϕ(n)\phi(n)Reduce k  ϕ(n)k \bmod \phi(n) only when gcd⁡(a,n)=1\gcd(a,n)=1ak≡ak  ϕ(n)(modn)a^k \equiv a^{k \bmod \phi(n)} \pmod n, valid because aϕ(n)≡1a^{\phi(n)}\equiv 1
True order of aa smaller than ϕ(n)\phi(n)Reduction modulo the true order is even more efficientOrder always divides ϕ(n)\phi(n), but need not equal it
Computing ϕ(n)\phi(n) without factoring nnPractically hard for large nn (this hardness is the basis of RSA)Only feasible when the factorization of nn is known

The mathematician behind it

Origins · 1 minLeonhard Euler and how this was discovered
LE
Leonhard Euler
1707–1783 · 18th-century Basel and St. Petersburg/Berlin Academies

Leonhard Euler was among the most prolific mathematicians in history, contributing foundational results across nearly every branch of mathematics then in existence, from analysis and mechanics to graph theory and number theory. His work on number theory, though only a fraction of his total output, includes the generalization of Fermat's Little Theorem that now bears his name, introduced in a paper of 1763 and refined further in subsequent decades; he also introduced the totient function ϕ(n)\phi(n) itself as the natural tool needed to state the generalization correctly for composite moduli.

History
- 1640: Pierre de Fermat states what is now called Fermat's Little Theorem, for prime moduli, without a full published proof.
- 1736: Leonhard Euler publishes the first surviving proof of Fermat's Little Theorem.
- 1763: Euler proves the generalization to composite moduli, introducing what becomes the totient function, establishing Euler's theorem.
- 1801: Carl Friedrich Gauss systematizes modular arithmetic and these results in Disquisitiones Arithmeticae, framing them within congruence notation still used today.
- 1978: Rivest, Shamir, and Adleman publish the RSA cryptosystem, whose correctness depends directly on Euler's theorem applied to composite moduli that are products of two primes.
Why does Euler's theorem require gcd⁡(a,n)=1\gcd(a,n)=1 instead of working for all integers aa?

The proof relies on multiplication by aa being a bijection on the set of residues coprime to nn. This bijectivity itself relies on aa being invertible modulo nn, which by Bézout's identity happens exactly when gcd⁡(a,n)=1\gcd(a,n)=1. If aa shares a factor with nn, multiplication by aa can collapse distinct residues onto the same image (it fails to be injective), so the permutation argument collapses. Concretely, as shown in the counterexample, a=5,n=10a=5, n=10 gives 5k≡5(mod10)5^k \equiv 5 \pmod{10} forever, never reaching 11, because 55 is a zero divisor modulo 1010 rather than a unit. The coprimality condition is precisely the condition that makes aa a genuine element of the finite group (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times.

How is Fermat's Little Theorem related to Euler's theorem?

Fermat's Little Theorem is the special case of Euler's theorem where the modulus nn is a prime pp. Since every integer from 11 to p−1p-1 is automatically coprime to a prime pp, the totient satisfies ϕ(p)=p−1\phi(p) = p-1, and Euler's theorem aϕ(p)≡1(modp)a^{\phi(p)} \equiv 1 \pmod p becomes ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p, which is exactly Fermat's statement. Historically the order ran the other way: Fermat stated the prime case first (around 1640), and Euler later realized the same permutation argument works for any modulus once ϕ(n)\phi(n) replaces p−1p - 1, producing the fully general theorem in 1763.

Can Euler's theorem be used to find a−1 mod na^{-1} \bmod n, the modular inverse of aa?

Yes. If gcd⁡(a,n)=1\gcd(a,n) = 1, Euler's theorem gives aϕ(n)≡1(modn)a^{\phi(n)} \equiv 1 \pmod n, which can be rewritten as a⋅aϕ(n)−1≡1(modn)a \cdot a^{\phi(n)-1} \equiv 1 \pmod n. This shows aϕ(n)−1  na^{\phi(n)-1} \bmod n is a multiplicative inverse of aa modulo nn, computable via fast modular exponentiation in O(log⁡ϕ(n))O(\log \phi(n)) multiplications. In practice, the Extended Euclidean Algorithm is often faster since it avoids needing to know ϕ(n)\phi(n), but the Euler-theorem approach is conceptually clean and is exactly how RSA decryption exponents relate to encryption exponents via inverses modulo ϕ(n)\phi(n).

Does repeated squaring change the mathematical value of ak mod na^k \bmod n, or just how fast we compute it?

It only changes the computational route, never the value. The theorem proved above shows the repeated-squaring procedure computes the same residue class as direct exponentiation followed by reduction mod nn; this is guaranteed by the laws of exponents (ak=∏a2ia^k = \prod a^{2^i} over the set bits of kk) together with the fact that reducing modulo nn at intermediate steps never changes the final congruence class, since congruence is preserved under multiplication. The entire benefit is algorithmic efficiency: replacing up to k−1k-1 multiplications with about 2log⁡2k2\log_2 k multiplications, while keeping every intermediate number bounded roughly by n2n^2 rather than growing without bound.

What happens if the exponent kk is negative or a≡0(modn)a \equiv 0 \pmod n?

Euler's theorem as stated requires gcd⁡(a,n)=1\gcd(a,n)=1, which already excludes a≡0(modn)a \equiv 0 \pmod n whenever n>1n > 1 (since gcd⁡(0,n)=n≠1\gcd(0,n) = n \ne 1). For negative exponents, a−1  na^{-1} \bmod n must first be computed (which requires gcd⁡(a,n)=1\gcd(a,n)=1, guaranteeing an inverse exists by Bézout's identity), and then a−k  na^{-k} \bmod n for k>0k>0 is computed as (a−1)k  n(a^{-1})^k \bmod n using the same repeated-squaring algorithm applied to the inverse. So negative exponents are handled by first inverting, not by any modification of the core theorem.

Why is factoring nn relevant to computing ϕ(n)\phi(n), and why does this matter for cryptography?

The formula ϕ(n)=n∏p∣n(1−1p)\phi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) requires knowing the distinct prime factors of nn. For a general large nn, factoring is believed to be computationally hard (no known efficient classical algorithm), so ϕ(n)\phi(n) is hard to compute without factoring nn first. RSA exploits exactly this asymmetry: the public modulus n=pqn=pq is published, but ϕ(n)=(p−1)(q−1)\phi(n) = (p-1)(q-1) can only be efficiently computed by someone who knows the prime factors p,qp,q, kept secret. Euler's theorem is what makes the decryption exponent work correctly once ϕ(n)\phi(n) is known, while the difficulty of finding ϕ(n)\phi(n) from nn alone is what keeps the scheme secure.

Is the converse of Euler's theorem true — if am≡1(modn)a^m \equiv 1 \pmod n for some mm, must gcd⁡(a,n)=1\gcd(a,n)=1?

Yes, and this direction is easy: if am≡1(modn)a^m \equiv 1 \pmod n for some positive integer mm, then n∣am−1n \mid a^m - 1, so a⋅am−1−1=am−1=tna \cdot a^{m-1} - 1 = a^m - 1 = tn for some integer tt, giving a⋅am−1−tn=1a\cdot a^{m-1} - tn = 1. This is an integer combination of aa and nn equal to 11, so by Bézout's identity (or its converse direction, which is elementary: any common divisor of aa and nn must divide 11) we conclude gcd⁡(a,n)=1\gcd(a,n) = 1. So coprimality is not just sufficient but also necessary for aa to have any finite multiplicative order modulo nn; this is proved directly here, not merely asserted.

Before this

Where this leads