Factoring 2873 and 1547 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(236−1,224−1), or when a fraction like n+4n2+2 can be reduced.
The key fact
If d divides both a and b, then d divides a−b, and more generally a−kb for any integer k. The reverse is true too. So a,b and a−kb,b have exactly the same common divisors, and therefore the same GCD.
The Euclidean algorithm
For any integers a, b, k:
gcd(a,b)=gcd(a−kb,b).
Choosing k to make a−kb the remainder amodb gives gcd(a,b)=gcd(b,amodb). Repeat until the remainder is 0; the last nonzero remainder is the GCD.
For gcd(1071,462):
1071462147=2⋅462+147=3⋅147+21=7⋅21+0
So gcd(1071,462)=21.
GCDs of expressions
The same move works when a and b are expressions in n: subtract a multiple of one from the other to kill the variable. What remains is a constant c, and then gcd must divide c.
For gcd(n2+3,n+2): subtract (n−2)(n+2)=n2−4 from n2+3 to get 7. So gcd(n2+3,n+2)=gcd(7,n+2), which is 7 when n≡5(mod7) and 1 otherwise. (Shortcut: n≡−2(modn+2), so n2+3≡4+3=7.)
Powers and Fibonacci numbers
Since 236−1=212(224−1)+(212−1), a step of the algorithm on 2a−1 and 2b−1 looks exactly like a step on the exponents a and b. So
gcd(xa−1,xb−1)=xgcd(a,b)−1
for any integer x≥2. The Fibonacci numbers (F1=F2=1, Fn+1=Fn+Fn−1) have the same property: gcd(Fa,Fb)=Fgcd(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) as ax+by for some integers x,y. This is Bézout's identity, and it is why ax+by=c is solvable whenever gcd(a,b)∣c. When gcd(a,m)=1, it also finds the inverse of a modulo m.
Worked example: Plain Euclid
Find gcd(2873,1547).
287315471326=1⋅1547+1326=1⋅1326+221=6⋅221+0
The GCD is 221. (Indeed 2873=13⋅221 and 1547=7⋅221.)
Worked example: A GCD of expressions
As n ranges over the positive integers, what is the largest possible value of gcd(n2+5,n+3)?
n2+5−(n−3)(n+3)=n2+5−n2+9=14. So gcd(n2+5,n+3)=gcd(14,n+3), which is at most 14. It equals 14 when 14∣n+3, for example n=11: gcd(126,14)=14. The answer is 14.
Worked example: Differences of powers
Find gcd(236−1,224−1).
gcd(36,24)=12, so the GCD is 212−1=4095.
Worked example: An inverse by back-substitution
Find the integer x with 0≤x<100 and 37x≡1(mod100).
Run the algorithm:
1003726114=2⋅37+26=1⋅26+11=2⋅11+4=2⋅4+3=1⋅3+1
Back-substitute, keeping everything in terms of 100 and 37:
So −27⋅37≡1(mod100) and x=100−27=73. Check: 37⋅73=2701.
Common mistake
When you reduce gcd(f(n),g(n)) to gcd(c,g(n)), the GCD divides c, but it doesn't have to equalc. Always check that some n 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)).
Practice
Practice 1
What is gcd(320−1,312−1)?
Practice 2
What is gcd(4199,3094)?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Practice 3
For how many integers n with 1≤n≤100 is gcd(n+7,2n+1)>1?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.
Practice 4
What is the smallest positive integer x such that 17x−1 is divisible by 60?
Practice 5
The Fibonacci numbers are F1=F2=1 and Fn+1=Fn+Fn−1. What is gcd(F60,F45)?
Practice 6
As n ranges over the positive integers, what is the largest possible value of gcd(n2+7,2n+1)?
Practice 7
For how many positive integers n≤1000 is the fraction n+4n2+2not in lowest terms?
Enter a number. Fractions like 3/4 and sqrt(2) are OK.