Learn / Math / Euclid, CRT & Euler
18. Euclid, CRT & Euler
The Bridge · Lesson 19 of 26 · 13:28
A faster way to find the gcd and the inverse for any modulus, not just a prime; how two small congruences combine into one big one through the Chinese Remainder Theorem; and Euler's theorem, the one formula that makes Fermat's little theorem from Topic 5 just its prime special case.

Topic 5 found modular inverses by brute force, and promised a shortcut for huge primes in Fermat’s little theorem. This lesson delivers the other half of that promise — a fast way to find the gcd and the inverse for any modulus, not only a prime one — and then builds two more tools, the Chinese Remainder Theorem and Euler’s theorem, that lean on exactly that machinery.

Euclid’s algorithm finds the gcd

Checking every number up to nn for a shared factor works, but it’s slow. Euclid’s algorithm finds the greatest common divisor in a handful of steps, no matter how big the numbers are, by repeatedly replacing a pair with a smaller one that has the same gcd.

finding the gcd by repeated division a = b × q + r — b and r become the next pair STEP 1 252 = 2 × 105 + 42 105 fits in 252 twice (210). Left over: 42. STEP 2 105 = 2 × 42 + 21 42 fits in 105 twice (84). Left over: 21. STEP 3 42 = 2 × 21 + 0 21 fits in 42 exactly. Left over: 0. Stop. gcd(252, 105) = 21 Check: 252 ÷ 21 = 12 and 105 ÷ 21 = 5 — both divide evenly. THE LAST NONZERO REMAINDER IS THE GCD Each step shrinks the numbers, so it finishes fast.
Fig. 1 — gcd(252, 105) by repeated division. Each step's divisor and remainder become the next step's pair, until a remainder of zero stops the chain.
Euclid's algorithm

gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b)

Divide aa by bb, keep the remainder, and repeat with the smaller pair. The last nonzero remainder is the gcd. Each step at least halves the numbers, so the whole chain finishes in roughly log⁡(n)\log(n) steps — fast enough for the 254-bit numbers this series actually uses.

Does 7 have an inverse mod 26?

The same division chain answers the question Topic 5 could only answer by brute force: does an inverse exist at all? Run it on (26,7)(26, 7) and keep every line — they’re needed again in a moment.

does 7 have an inverse mod 26? run the same division chain on (26, 7) STEP 1 26 = 3 × 7 + 5 KEEP STEP 2 7 = 1 × 5 + 2 KEEP STEP 3 5 = 2 × 2 + 1 KEEP STEP 4 2 = 2 × 1 + 0 remainder 0 — just confirms the chain is done gcd(26, 7) = 1 → an inverse exists 26 and 7 share no factor except 1. KEEP EVERY LINE — WE NEED THEM NEXT Part 2 uses these three lines to find the inverse.
Fig. 2 — the forward Euclid pass on (26, 7). Three steps land on a nonzero remainder and get kept; the last step just confirms the chain is done.

The last nonzero remainder is 11, so gcd⁡(26,7)=1\gcd(26, 7) = 1 and 77 has an inverse mod 2626 — exactly the condition from Topic 5, reached in three steps instead of twenty-five candidates.

Working backwards to find the inverse

Running Euclid forward finds that an inverse exists. Running the same three lines backwards — substituting each remainder back into the equation before it — finds the inverse itself, with no searching at all.

working backwards to find the inverse THE THREE LINES FROM PART 1 26 = 3×7 + 5 7 = 1×5 + 2 5 = 2×2 + 1 (line 1) (line 2) (line 3) STEP 1 — start from line 3 1 = 5 − 2×2 STEP 2 — replace 2 using line 2 (2 = 7 − 5) 1 = 3×5 − 2×7 STEP 3 — replace 5 using line 1 (5 = 26 − 3×7) 1 = 3×26 − 11×7 STEP 4 — drop the 26-term (it's 0 mod 26) 1 ≡ −11×7 (mod 26) STEP 5 — add 26 to make the coefficient positive −11 + 26 = 15 7-1 ≡ 15 (mod 26) Check: 7 × 15 = 105 = 4 × 26 + 1. THE INVERSE OF 7 MOD 26 IS 15 This works for any modulus, not just a prime.
Fig. 3 — back-substituting the three kept lines isolates 1 as a combination of 26 and 7. Reading off the 7-coefficient, made positive, is the inverse.
The extended Euclidean algorithm

ax+ny=gcd⁡(a,n)a x + n y = \gcd(a, n)

Back-substitution finds integers xx and yy satisfying this equation — called Bézout’s identity. When gcd⁡(a,n)=1\gcd(a, n) = 1, that equation reads ax+ny=1ax + ny = 1, which is exactly ax≡1(modn)ax \equiv 1 \pmod n: xx is the inverse, read straight off the last line.

When an inverse does not exist

Not every pair cooperates. Check 6 mod 96 \bmod 9 against every xx from 11 to 99, and the pattern Euclid’s algorithm already predicts shows up directly.

when an inverse does not exist does 6 have an inverse mod 9? STEP 1 — check every 6x mod 9 x=1 6 ≡6 x=2 12 ≡3 x=3 18 ≡0 x=4 24 ≡6 x=5 30 ≡3 x=6 36 ≡0 x=7 42 ≡6 x=8 48 ≡3 x=9 54 ≡0 no x ever makes 6x ≡ 1 (mod 9) STEP 2 — Euclid's algorithm explains why 9 = 1 × 6 + 3 6 = 2 × 3 + 0 gcd(9, 6) = 3, not 1 so 6 has no inverse mod 9 Compare: gcd(26, 7) = 1, so 7 does have an inverse mod 26. AN INVERSE EXISTS ONLY WHEN THE GCD IS 1 With a prime modulus, every nonzero number qualifies.
Fig. 4 — every product 6x mod 9 is checked and none ever lands on 1. Euclid's algorithm explains why: gcd(9, 6) = 3, not 1.
When an inverse exists

gcd⁡(a,n)=1\gcd(a, n) = 1

Same condition as Topic 5, now checkable in three division steps instead of a brute-force search of size nn. With a prime modulus this is automatic — every number from 11 to p−1p - 1 shares no factor with pp — which is exactly why ZK systems build their field out of a prime.

Two clues about one number

The Chinese Remainder Theorem answers a different question: given two separate clues about a number — its remainder mod 33 and its remainder mod 55 — is there a number that satisfies both, and how many are there?

two clues about one number x ≡ 2 (mod 3) and x ≡ 3 (mod 5) STEP 1 — list numbers that satisfy the mod-5 clue 38131823 STEP 2 — check each against the mod-3 clue ≡0 (mod 3) ≡2 (mod 3) ✓ matches ≡1 (mod 3) ≡0 (mod 3) ≡2 (mod 3) ✓ matches STEP 3 — the matches repeat every 15 (3 × 5) 8 +15 23 +15 38 x ≡ 8 (mod 15) Check: 8 mod 3 = 2 and 8 mod 5 = 3 — both clues hold. ONE ANSWER MOD 15 FITS BOTH CLUES The two moduli, 3 and 5, share no factor.
Fig. 5 — listing numbers that satisfy one clue, then checking each against the other. The matches repeat every 15 — the product of the two moduli.

There’s exactly one answer in every run of 15=3×515 = 3 \times 5 consecutive numbers, because 33 and 55 share no factor. That’s the condition the Chinese Remainder Theorem needs: the moduli have to be pairwise coprime.

Building x without searching

Searching works for small moduli, but the CRT formula builds the same answer directly, using the inverses this lesson just made fast.

building x without searching m1=3, a1=2 m2=5, a2=3 M=15 STEP 1 — find each M M1 = 15 ÷ 3 = 5 M2 = 15 ÷ 5 = 3 STEP 2 — invert each M mod its own modulus 5-1 mod 3 = 2 3-1 mod 5 = 2 STEP 3 — combine both terms x = 2 × 5 × 2 + 3 × 3 × 2 = 20 + 18 = 38 STEP 4 — reduce mod M 38 mod 15 = 8 same answer: x ≡ 8 (mod 15) EACH TERM KEEPS ITS OWN REMAINDER, ZEROES THE OTHER The inverses from the last lesson do the work.
Fig. 6 — the CRT formula, built from the same two clues as the search. Each term is scaled so it keeps its own remainder and vanishes under the other modulus.
The CRT formula

x≡a1M1M1−1+a2M2M2−1(modM)x \equiv a_1 M_1 M_1^{-1} + a_2 M_2 M_2^{-1} \pmod{M}

M=m1m2M = m_1 m_2, and Mi=M/miM_i = M / m_i. Because MiM_i is a multiple of every modulus except mim_i, the term aiMiMi−1a_i M_i M_i^{-1} keeps remainder aia_i under mim_i and vanishes to 00 under every other modulus — so the sum satisfies every congruence at once.

φ(n) counts the numbers coprime to n

Euler’s theorem — the next and last tool in this lesson — needs one more idea first: how many numbers from 11 to nn share no factor with nn at all.

ϕ(n) counts the numbers coprime to n numbers from 1 to n that share no factor with n n = 9 ϕ(9) = 6 123456789 n = 12 ϕ(12) = 4 123456789101112 n = 15 ϕ(15) = 8 123456789101112131415 ϕ(p) = p − 1 ϕ(p×q) = (p−1)(q−1) Both when p and q are prime — p×q needs p and q distinct. ϕ(n) COUNTS THE NUMBERS COPRIME TO n Coprime means the gcd with n is 1.
Fig. 7 — φ(n), the count of numbers from 1 to n coprime to n, across three moduli, plus the two shortcut formulas for computing it.
Euler's totient function

φ(p)=p−1,φ(p×q)=(p−1)(q−1)\varphi(p) = p - 1, \qquad \varphi(p \times q) = (p-1)(q-1)

The first formula holds for any prime pp — every nonzero number below it qualifies. The second holds whenever pp and qq are distinct primes, which is exactly the structure behind an RSA modulus n=p×qn = p \times q.

Euler’s theorem

With φ(n)\varphi(n) in hand, Euler’s theorem generalizes Fermat’s little theorem from a prime modulus to any modulus — as long as the base and the modulus share no factor.

Euler's theorem raise a to ϕ(n) and the result always lands on 1 aϕ(n) ≡ 1 (mod n) when gcd(a, n) = 1 STEP 1 — n=9, a=2, ϕ(9)=6 — compute a to the ϕ(n) 26 = 64 STEP 2 — reduce mod 9 64 = 7×9 + 1 → 26 ≡ 1 STEP 3 — split one factor off to get the inverse 2 × 25 ≡ 1 → 2-1 ≡ 32 ≡ 5 Check: 2 × 5 = 10 ≡ 1 (mod 9). When n is a prime p, ϕ(p) = p − 1 — this is exactly Fermat's little theorem. FERMAT IS EULER'S THEOREM FOR A PRIME Euler works for any modulus, as long as the gcd is 1.
Fig. 8 — Euler's theorem worked through n=9, a=2. Splitting the exponent by one gives the inverse directly, the same trick Fermat's little theorem used for a prime.
Euler's theorem

aφ(n)≡1(modn),whenever gcd⁡(a,n)=1a^{\varphi(n)} \equiv 1 \pmod{n}, \qquad \text{whenever } \gcd(a, n) = 1

Set nn to a prime pp: φ(p)=p−1\varphi(p) = p - 1, and the statement collapses to ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p — Fermat’s little theorem from Topic 5, now visibly just the prime special case of this more general rule.

Faster inverses, bigger numbers, one big idea

Three tools, each building on the last: Euclid’s algorithm for a fast gcd and inverse at any modulus, the Chinese Remainder Theorem for combining several small moduli into one big answer, and Euler’s theorem for the exponent rule that holds everywhere Fermat’s little theorem used to need a prime.

three tools, one bridge to the next topics EUCLID finds gcd(a, n) fast, and the inverse of a mod any n replaces the brute-force search from Modular Inverses (Topic 5) works for any modulus, not only a prime one CRT splits one big modulus into several small coprime ones used for fast modular exponentiation (the RSA speedup) batching many small checks into one combined answer EULER aϕ(n) ≡ 1 (mod n) whenever gcd(a, n) = 1 generalizes Fermat's little theorem, its prime special case (Topic 5) to any modulus, prime or composite FASTER INVERSES, BIGGER NUMBERS, ONE BIG IDEA Next up: subgroups, cosets, and roots of unity.
Fig. 9 — the three tools this lesson adds, and what each one is for.

Every one of these is really a statement about the structure hiding underneath modular arithmetic — the sets of numbers that behave consistently under multiplication. Making that structure explicit, with groups, subgroups, cosets, and roots of unity, is exactly where the next lessons go.

Key takeaways
  • Euclid’s algorithm finds the gcd in roughly log⁡(n)\log(n) steps by repeatedly replacing a pair of numbers with a smaller pair sharing the same gcd.
  • Running Euclid’s steps backwards — the extended Euclidean algorithm — finds the modular inverse directly, with no brute-force search.
  • An inverse exists exactly when gcd⁡(a,n)=1\gcd(a, n) = 1, which is automatic for every nonzero value whenever nn is prime.
  • The Chinese Remainder Theorem combines two congruences with pairwise-coprime moduli into one answer, using a direct formula built from these same fast inverses.
  • Euler’s theorem (aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod n) generalizes Fermat’s little theorem to any modulus — Fermat’s is just its prime special case.