Lemma › Guides › gcd(2^m − 1, 2^n − 1): the proof, and how to spot it
Guide

gcd(2^m − 1, 2^n − 1): the proof, and how to spot it

12 October 2026

Nobody gets stuck on this problem because the algebra is hard. Every line of the proof is a factorisation you already know. People get stuck because they try to attack 2m−12^m - 1 and 2n−12^n - 1 as numbers, factoring them, hunting for primes, and the numbers are the wrong thing to look at.

The whole proof is one observation: the Euclidean algorithm on the exponents produces the Euclidean algorithm on the numbers, step for step. Once you see that, the identity

gcd⁡(2m−1, 2n−1)=2gcd⁡(m,n)−1\gcd(2^m - 1,\ 2^n - 1) = 2^{\gcd(m,n)} - 1

stops being a fact to memorise and becomes something you can rebuild in two minutes in an exam. This guide shows the proof, a second proof by orders, and the problems where it quietly does the work.

Believe it first

Take m=18m = 18 and n=12n = 12. Then 218−1=2621432^{18} - 1 = 262143 and 212−1=40952^{12} - 1 = 4095. Run Euclid on those: 262143262143 leaves remainder 6363 on division by 40954095, and 4095=63⋅654095 = 63 \cdot 65, so the gcd is 6363.

Now look at the exponents. gcd⁡(18,12)=6\gcd(18, 12) = 6, and 26−1=632^6 - 1 = 63. Same answer.

Look closer at that first remainder. Euclid on the exponents says 18=1⋅12+618 = 1 \cdot 12 + 6, remainder 66. Euclid on the numbers gave remainder 63=26−163 = 2^6 - 1. The remainder on the numbers is 22 to the remainder on the exponents, minus one. That is not a coincidence, and it is the entire proof.

The one fact you need

If dd divides nn, then 2d−12^d - 1 divides 2n−12^n - 1. Write n=dkn = dk and x=2dx = 2^d; then 2n−1=xk−12^n - 1 = x^k - 1, and x−1x - 1 always divides xk−1x^k - 1, because

xk−1=(x−1)(xk−1+xk−2+⋯+1).x^k - 1 = (x - 1)(x^{k-1} + x^{k-2} + \dots + 1).

You have probably met this already. It is the same factorisation that shows if 2n−12^n - 1 is prime then nn is prime: a composite exponent hands you a proper factor for free.

The proof

Take m≥nm \ge n and divide: m=qn+rm = qn + r with 0≤r<n0 \le r < n. Split off the remainder in the exponent:

2m−1=2r(2qn−1)+(2r−1).2^m - 1 = 2^r\left(2^{qn} - 1\right) + \left(2^r - 1\right).

Expand the right side if you doubt it: 2r+qn−2r+2r−12^{r+qn} - 2^r + 2^r - 1. Now nn divides qnqn, so by the fact above 2n−12^n - 1 divides 2qn−12^{qn} - 1. That kills the first term modulo 2n−12^n - 1, leaving

2m−1≡2r−1(mod2n−1).2^m - 1 \equiv 2^r - 1 \pmod{2^n - 1}.

And a gcd does not change when you replace one number by its remainder modulo the other. So

gcd⁡(2m−1, 2n−1)=gcd⁡(2n−1, 2r−1).\gcd(2^m - 1,\ 2^n - 1) = \gcd(2^n - 1,\ 2^r - 1).

The pair of exponents went from (m,n)(m, n) to (n,r)(n, r), which is exactly one step of Euclid on mm and nn. Repeat. The exponents follow Euclid's algorithm all the way down until the remainder is 00, at which point the exponent pair is (gcd⁡(m,n),0)(\gcd(m,n), 0), and the numbers are 2gcd⁡(m,n)−12^{\gcd(m,n)} - 1 and 20−1=02^0 - 1 = 0. The gcd of anything with 00 is itself. Done.

On the example: (18,12)→(12,6)→(6,0)(18, 12) \to (12, 6) \to (6, 0) on the exponents, and (262143,4095)→(4095,63)→(63,0)(262143, 4095) \to (4095, 63) \to (63, 0) on the numbers. Every exponent ee on the left becomes 2e−12^e - 1 on the right.

This is the problem in the archive, and when you write it up, the marker is looking for two things: the congruence with a reason attached, and an honest ending at remainder 00 rather than "and so on".

A second proof, by orders

The first proof is the one to write. This one is worth knowing because it generalises to problems where Euclid has nothing to run on, and it is pure modular thinking.

Let g=gcd⁡(2m−1,2n−1)g = \gcd(2^m - 1, 2^n - 1). Then modulo gg we have both 2m≡12^m \equiv 1 and 2n≡12^n \equiv 1. Any power of 22 built by adding and subtracting multiples of mm and nn is therefore also ≡1\equiv 1. By Bézout, gcd⁡(m,n)\gcd(m,n) can be written as am−bnam - bn with a,b≥0a, b \ge 0 (for 1818 and 1212: 18−12=618 - 12 = 6). So 2am≡2bn⋅2gcd⁡(m,n)2^{am} \equiv 2^{bn} \cdot 2^{\gcd(m,n)}, and since both 2am2^{am} and 2bn2^{bn} are ≡1\equiv 1, we get 2gcd⁡(m,n)≡1(modg)2^{\gcd(m,n)} \equiv 1 \pmod g. That is, gg divides 2gcd⁡(m,n)−12^{\gcd(m,n)} - 1.

The other direction is the one fact from earlier: gcd⁡(m,n)\gcd(m,n) divides both mm and nn, so 2gcd⁡(m,n)−12^{\gcd(m,n)} - 1 divides both numbers, hence divides gg. Each divides the other, so they are equal.

How to spot it next time

The identity hides behind several disguises. Reach for it when you see:

One warning. The plus version is not the same. 22+1=52^2 + 1 = 5 and 24+1=172^4 + 1 = 17 are coprime, even though gcd⁡(2,4)=2\gcd(2, 4) = 2. Only the minus sign gives you the clean factorisation the whole argument rests on.

What to write in the exam

Four lines are enough:

  1. Divide with remainder: m=qn+rm = qn + r.
  2. State the congruence 2m−1≡2r−1(mod2n−1)2^m - 1 \equiv 2^r - 1 \pmod{2^n - 1}, with the factorisation as its reason.
  3. Conclude the gcd is unchanged when (m,n)(m, n) becomes (n,r)(n, r).
  4. Say the process is Euclid's algorithm on the exponents and finishes at (gcd⁡(m,n),0)(\gcd(m,n), 0).

If you can produce those four lines from memory, you do not need to remember the formula. You need to remember that exponents run Euclid too.

Related guides

Train it on Lemma

Every technique here is taught explicitly, in order. 125 lessons, 1206 curated problems and unlimited generated practice at six difficulties. Free to start, no card, and every paid plan opens with 3 free days.

Find your level