Common Divisor
A common divisor of two integers a and b is any integer d such that both d∣a and d∣b.
Example. Which of 1, −6 and 9 are common divisors of 12 and 18?
Since 1∣12 and 1∣18, 1 is a common divisor; since −6∣12 and −6∣18, −6 is also a common divisor; but 9∤12, so 9 is not.
Note
GCD
A greatest common divisor (GCD) of two integers a and b is any integer d such that
d∣a and d∣b
and
for all c∈Z,if c∣a and c∣b, then c∣d.
Here, both 6 and -6 are GCDs of 12 and 18.
Note
Standard GCD
The standard greatest common divisor of two integers a and b (not both 0), written as gcd(a,b) is the integer d such that
d∣a and d∣b
and
for all c∈Z,if c∣a and c∣b, then c≤d.
Here, gcd(12,18)=6.
Note
Coprime
Two integers a and b are coprime if they have no common divisors other than ±1. In other words, gcd(a,b)=1.
Example. Which of the following pairs are coprime? 2 and 3 are coprime; 9 and −10 are coprime; 12 and 18 are not coprime, since 6 divides both; 0 and 1 are coprime, since the only divisors of 1 are ±1.
Note
Lemma 1
For any integer a, gcd(a,1)=1.
Note
Lemma 2
For any integer a, gcd(a,0)=∣a∣.
Suppose d is the greatest divisor of a and 0, therefore d∣a and d∣0; d≥0.
Since we are looking at the greatest divisor, d=a is the strongest candidate that satisfies d∣a. However, d≥0, so instead we can assume that d=∣a∣. Since every integer divides 0, we know that ∣a∣∣0, therefore we know that gcd(a,0)=∣a∣.
Lemmas 1 and 2 are actually just the first two of a family of useful properties of the standard GCD. Suppose a, b, c and q are integers; then the following all hold.
Note
Lemma 3
For any integers a, b and c,
gcd(a,gcd(b,c))=gcd(gcd(a,b),c).
Basically, gcd is associative; if you need the GCD of three (or more) numbers, you can pair them up in whatever order you like and the answer does not change. The proof is a Topic 1 problem (Q12), so I'm not gonna write it here.
Example. Find the GCD of 12, 18 and 30.
Pairing from the right,
gcd(12,gcd(18,30))=gcd(12,6)=6.
Pairing from the left,
gcd(gcd(12,18),30)=gcd(6,30)=6.
Both orders agree, so the GCD of all three numbers is 6.
Note
Lemma 4
For any integers a, b and c,
gcd(ac,bc)=∣c∣gcd(a,b).
Basically, a shared factor can be pulled straight out the front of a gcd; just remember the absolute value, since the standard GCD is never negative.
Example. Find gcd(24,60).
Notice that 24 and 60 share the factor 12, so
gcd(24,60)=gcd(2×12,5×12)=∣12∣gcd(2,5)=12×1=12.
The same works for negative shared factors; gcd(−15,−25)=∣−5∣gcd(3,5)=5×1=5.
Note
Lemma 5
For any integers a, b and c, if a∣bc and gcd(a,b)=1, then a∣c.
Basically, if a divides a product but is coprime to one of the factors, then it has no choice but to divide the other factor; none of a can "hide" inside b, so all of it must land on c.
Example. Suppose 7∣3c for some integer c. What can we say about c?
Since gcd(7,3)=1, Lemma 5 tells us that 7∣c. For instance, if 3c=42, then c=14, and indeed 7∣14.
It is important to remember that Lemma 5 fails without the coprime condition. For example, 6∣4×3, but gcd(6,4)=2=1, and sure enough 6∤4 and 6∤3.
The last property is the engine behind the Euclidean Algorithm, so it gets its own proof.
Note
Lemma 6 For any integers a, b, c and q, if a=qb+c, then
gcd(a,b)=gcd(b,c).
Proof. We show that the pairs (a,b) and (b,c) have exactly the same common divisors; if the two sets of common divisors are identical, then in particular their greatest elements must be equal.
Suppose d∣a and d∣b. Since c=a−qb is an integer combination of a and b, we get d∣c; so every common divisor of a and b is also a common divisor of b and c.
Conversely, suppose d∣b and d∣c. Since a=qb+c is an integer combination of b and c, we get d∣a; so every common divisor of b and c is also a common divisor of a and b.
Hence the two pairs share the same common divisors, and therefore the same greatest common divisor; that is, gcd(a,b)=gcd(b,c). ■
Basically, replacing a with its remainder on division by b does not change the GCD; this lets us shrink a GCD problem into a smaller one over and over again until it becomes trivial, which is exactly what the Euclidean Algorithm does.
Example. Show that any two consecutive integers are coprime, and hence find gcd(2026,2025).
For any integer n we can write
n+1=1×n+1,
so by Lemma 6,
gcd(n+1,n)=gcd(n,1)=1, (by Lemma 1).
Therefore any two consecutive integers are coprime; in particular, gcd(2026,2025)=1, with no arithmetic needed at all.
A common trap with negative a is to write −30=−7×4−2; this is arithmetically true, but it is NOT the Division Theorem, because the remainder −2 is negative. Instead, we push the quotient one further down to −8 so that the remainder lands back in the range 0≤r<∣b∣. Basically, for negative a you round the quotient towards −∞, not towards 0.
Proof. First suppose b>0. Choose
q=⌊ba⌋ and r=a−qb,
i.e. q is the largest integer less than or equal to ba. Then a=qb+r holds by construction, and since
q≤ba<q+1,
multiplying through by b and then subtracting qb gives
0≤a−qb<b, that is, 0≤r<b,
as required. The case b<0 works the same way, except we instead take q=⌈ba⌉.
For uniqueness, suppose there were a second solution a=q′b+r′ with 0≤r′<∣b∣. Setting the two expressions for a equal,
qb+r(q−q′)b=q′b+r′=r′−r.
Since both remainders lie in the interval [0,∣b∣), their difference satisfies −∣b∣<r′−r<∣b∣. But r′−r=(q−q′)b is an integer multiple of b, and the only multiple of b strictly between −∣b∣ and ∣b∣ is 0. Hence r=r′ and q=q′; the quotient and remainder are unique. ■
The Euclidean Algorithm The Euclidean Algorithm is a process that, given two integers a and b=0 as inputs, efficiently outputs gcd(a,b). The algorithm makes use of the Division Theorem, finding quotients and remainders iteratively in the following way:
abr0r1rn−2rn−1=q0×b+r0=q1×r0+r1=q2×r1+r2=q3×r2+r3⋮=qn×rn−1+rn=qn+1×rn+0where q0,r0∈Z and ∣b∣>r0≥0,where q1,r1∈Z and r0>r1≥0,where q2,r2∈Z and r1>r2≥0,where q3,r3∈Z and r2>r3≥0,⋮where qn,rn∈Z and rn−1>rn≥0,where qn+1∈Z and rn>0.
The process terminates immediately after the nth step, when the remainder is first found to be zero. The remainder at the nth step is then the GCD of a and b. That is,
gcd(a,b)=rn.
Basically, you divide a by b, then divide b by the remainder, then divide that remainder by the new remainder, and so on; each line just feeds the two right-most numbers of the line above back into the Division Theorem. The remainders keep strictly shrinking, so eventually one hits 0, and the last non-zero remainder is the GCD.
Example. Use the Euclidean Algorithm to find gcd(403,286).
Since an integer and its negative have exactly the same divisors, we have
gcd(−a,b)=gcd(a,b)=gcd(a,−b),
so the easiest way to deal with a negative input is to just drop the sign before starting.
Example. Find gcd(−408,126).
Dropping the sign first, gcd(−408,126)=gcd(408,126), so
40812630=3×126+30,=4×30+6,=5×6+0.
Therefore gcd(−408,126)=6.
Alternatively, the algorithm works perfectly fine on the negative input directly, as long as every remainder stays in the range 0≤r<∣b∣ (recall that the Division Theorem forces r≥0 even when the quotient goes negative);
−4081269630=−4×126+96,=1×96+30,=3×30+6,=5×6+0.
Both routes agree that the answer is 6; dropping the sign first is simply less error-prone.
Theorem For any integer inputs a and b=0, the Euclidean Algorithm always outputs gcd(a,b).
Proof. There are two things to show; that the process always terminates, and that it always returns the standard GCD.
For termination, notice that the remainders satisfy
∣b∣>r0>r1>r2>⋯≥0,
so they form a strictly decreasing sequence of non-negative integers; such a sequence cannot go on forever, so a zero remainder must appear after at most ∣b∣ steps.
For correctness, every line of the algorithm has exactly the form of Lemma 6, so each step replaces the current pair with a smaller pair that has the same GCD. If the algorithm terminates after n steps, then
Therefore gcd(89,55)=1; any two consecutive Fibonacci numbers are coprime. Notice how every quotient is 1 until the final step, and the remainders are just the Fibonacci numbers marching back down (34,21,13,8,5,3,2,1); consecutive Fibonacci numbers are actually the worst case input for the Euclidean Algorithm, forcing the maximum possible number of steps for numbers of their size.
Example. A rectangular floor is 462 cm by 330 cm. What is the side length of the largest square tile that can tile the floor exactly, and how many tiles are needed?
A square tile of side s tiles the floor exactly if and only if s∣462 and s∣330; the largest such s is precisely gcd(462,330).
462330132=1×330+132,=2×132+66,=2×66+0.
So the largest tile has side 66 cm, and the number of tiles needed is
66462×66330=7×5=35.
Therefore, the largest square tile is 66 cm ×66 cm, and 35 tiles are needed.