Math Core

Module 3.4 · Number Theory

The Euclidean algorithm

Factoring 28732873 and 15471547 to find their GCD is slow. The Euclidean algorithm finds it in a few subtractions, with no factoring at all. More importantly for the AMC, the same idea handles GCDs of expressions: gcd⁡(n2+5,n+3)\gcd(n^2 + 5, n + 3), gcd⁡(236−1,224−1)\gcd(2^{36} - 1, 2^{24} - 1), or when a fraction like n2+2n+4\dfrac{n^2 + 2}{n + 4} can be reduced.

The key fact

If dd divides both aa and bb, then dd divides a−ba - b, and more generally a−kba - kb for any integer kk. The reverse is true too. So a,ba, b and a−kb,ba - kb, b have exactly the same common divisors, and therefore the same GCD.

The Euclidean algorithm

For any integers aa, bb, kk:

gcd⁡(a,b)=gcd⁡(a−kb, b).\gcd(a, b) = \gcd(a - kb,\ b).

Choosing kk to make a−kba - kb the remainder a mod ba \bmod b gives gcd⁡(a,b)=gcd⁡(b, a mod b)\gcd(a, b) = \gcd(b,\ a \bmod b). Repeat until the remainder is 00; the last nonzero remainder is the GCD.

For gcd⁡(1071,462)\gcd(1071, 462):

1071=2⋅462+147462=3⋅147+21147=7⋅21+0\begin{aligned} 1071 &= 2 \cdot 462 + 147 \\ 462 &= 3 \cdot 147 + 21 \\ 147 &= 7 \cdot 21 + 0 \end{aligned}

So gcd⁡(1071,462)=21\gcd(1071, 462) = 21.

GCDs of expressions

The same move works when aa and bb are expressions in nn: subtract a multiple of one from the other to kill the variable. What remains is a constant cc, and then gcd⁡\gcd must divide cc.

For gcd⁡(n2+3, n+2)\gcd(n^2 + 3,\ n + 2): subtract (n−2)(n+2)=n2−4(n - 2)(n + 2) = n^2 - 4 from n2+3n^2 + 3 to get 77. So gcd⁡(n2+3,n+2)=gcd⁡(7,n+2)\gcd(n^2 + 3, n + 2) = \gcd(7, n + 2), which is 77 when n≡5(mod7)n \equiv 5 \pmod 7 and 11 otherwise. (Shortcut: n≡−2(modn+2)n \equiv -2 \pmod{n+2}, so n2+3≡4+3=7n^2 + 3 \equiv 4 + 3 = 7.)

Powers and Fibonacci numbers

Since 236−1=212(224−1)+(212−1)2^{36} - 1 = 2^{12}(2^{24} - 1) + (2^{12} - 1), a step of the algorithm on 2a−12^a - 1 and 2b−12^b - 1 looks exactly like a step on the exponents aa and bb. So

gcd⁡(xa−1, xb−1)=xgcd⁡(a,b)−1\gcd(x^a - 1,\ x^b - 1) = x^{\gcd(a, b)} - 1

for any integer x≥2x \ge 2. The Fibonacci numbers (F1=F2=1F_1 = F_2 = 1, Fn+1=Fn+Fn−1F_{n+1} = F_n + F_{n-1}) have the same property: gcd⁡(Fa,Fb)=Fgcd⁡(a,b)\gcd(F_a, F_b) = F_{\gcd(a, b)}.

Running it backward: Bézout

Each remainder in the algorithm is a combination of the two original numbers. Substituting back up the chain writes gcd⁡(a,b)\gcd(a, b) as ax+byax + by for some integers x,yx, y. This is Bézout's identity, and it is why ax+by=cax + by = c is solvable whenever gcd⁡(a,b)∣c\gcd(a, b) \mid c. When gcd⁡(a,m)=1\gcd(a, m) = 1, it also finds the inverse of aa modulo mm.

Worked example: Plain Euclid

Find gcd⁡(2873,1547)\gcd(2873, 1547).

2873=1⋅1547+13261547=1⋅1326+2211326=6⋅221+0\begin{aligned} 2873 &= 1 \cdot 1547 + 1326 \\ 1547 &= 1 \cdot 1326 + 221 \\ 1326 &= 6 \cdot 221 + 0 \end{aligned}

The GCD is 221221. (Indeed 2873=13⋅2212873 = 13 \cdot 221 and 1547=7⋅2211547 = 7 \cdot 221.)

Worked example: A GCD of expressions

As nn ranges over the positive integers, what is the largest possible value of gcd⁡(n2+5, n+3)\gcd(n^2 + 5,\ n + 3)?

n2+5−(n−3)(n+3)=n2+5−n2+9=14n^2 + 5 - (n - 3)(n + 3) = n^2 + 5 - n^2 + 9 = 14. So gcd⁡(n2+5,n+3)=gcd⁡(14,n+3)\gcd(n^2 + 5, n + 3) = \gcd(14, n + 3), which is at most 1414. It equals 1414 when 14∣n+314 \mid n + 3, for example n=11n = 11: gcd⁡(126,14)=14\gcd(126, 14) = 14. The answer is 1414.

Worked example: Differences of powers

Find gcd⁡(236−1, 224−1)\gcd(2^{36} - 1,\ 2^{24} - 1).

gcd⁡(36,24)=12\gcd(36, 24) = 12, so the GCD is 212−1=40952^{12} - 1 = 4095.

Worked example: An inverse by back-substitution

Find the integer xx with 0≤x<1000 \le x \lt 100 and 37x≡1(mod100)37x \equiv 1 \pmod{100}.

Run the algorithm:

100=2⋅37+2637=1⋅26+1126=2⋅11+411=2⋅4+34=1⋅3+1\begin{aligned} 100 &= 2 \cdot 37 + 26 \\ 37 &= 1 \cdot 26 + 11 \\ 26 &= 2 \cdot 11 + 4 \\ 11 &= 2 \cdot 4 + 3 \\ 4 &= 1 \cdot 3 + 1 \end{aligned}

Back-substitute, keeping everything in terms of 100100 and 3737:

1=4−3=4−(11−2⋅4)=3⋅4−11=3(26−2⋅11)−11=3⋅26−7⋅11=3⋅26−7(37−26)=10⋅26−7⋅37=10(100−2⋅37)−7⋅37=10⋅100−27⋅37.\begin{aligned} 1 &= 4 - 3 = 4 - (11 - 2 \cdot 4) = 3 \cdot 4 - 11 \\ &= 3(26 - 2 \cdot 11) - 11 = 3 \cdot 26 - 7 \cdot 11 \\ &= 3 \cdot 26 - 7(37 - 26) = 10 \cdot 26 - 7 \cdot 37 \\ &= 10(100 - 2 \cdot 37) - 7 \cdot 37 = 10 \cdot 100 - 27 \cdot 37. \end{aligned}

So −27⋅37≡1(mod100)-27 \cdot 37 \equiv 1 \pmod{100} and x=100−27=73x = 100 - 27 = 73. Check: 37⋅73=270137 \cdot 73 = 2701.

Common mistake

When you reduce gcd⁡(f(n),g(n))\gcd(f(n), g(n)) to gcd⁡(c,g(n))\gcd(c, g(n)), the GCD divides cc, but it doesn't have to equal cc. Always check that some nn actually reaches the value you claim, and watch out for a step where you multiplied one expression by a constant: that can introduce extra factors (see the practice problem about gcd⁡(n2+7,2n+1)\gcd(n^2 + 7, 2n + 1)).

Practice

Practice 1

What is gcd⁡(320−1, 312−1)\gcd(3^{20} - 1,\ 3^{12} - 1)?

Practice 2

What is gcd⁡(4199,3094)\gcd(4199, 3094)?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

For how many integers nn with 1≤n≤1001 \le n \le 100 is gcd⁡(n+7, 2n+1)>1\gcd(n + 7,\ 2n + 1) \gt 1?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

What is the smallest positive integer xx such that 17x−117x - 1 is divisible by 6060?

Practice 5

The Fibonacci numbers are F1=F2=1F_1 = F_2 = 1 and Fn+1=Fn+Fn−1F_{n+1} = F_n + F_{n-1}. What is gcd⁡(F60,F45)\gcd(F_{60}, F_{45})?

Practice 6

As nn ranges over the positive integers, what is the largest possible value of gcd⁡(n2+7, 2n+1)\gcd(n^2 + 7,\ 2n + 1)?

Practice 7

For how many positive integers n≤1000n \le 1000 is the fraction n2+2n+4\dfrac{n^2 + 2}{n + 4} not in lowest terms?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Real contest practice