Greatest Common Divisor (Discrete Math)
Greatest Common Divisor (Discrete Math)
Greatest Common Divisor (Discrete Math)
3 Gallon Jug
5 Gallon Jug
Lecture 8: Sep 29
This Lecture
In this lecture we will learn the Euclidean algorithm for computing greatest common divisor (GCD), which is one of the earliest important algorithms. Then we use the Euclidean algorithm to derive an important result in number theory, which is the basic in elementary number theory.
Quotient remainder theorem Greatest common divisor & Euclidean algorithm Linear combination and GCD, extended Euclidean algorithm Prime factorization and other applications
We also say
q = a div b
and
r = a mod b.
When b=2, this says that for any a, there is a unique q such that a=2q or a=2q+1.
When b=3, this says that for any a, there is a unique q such that a=3q or a=3q+1 or a=3q+2.
Given any b, we can divide the integers into many blocks of b numbers. For any a, there is a unique position for a in this line. q = the block where a is in r = the offset in this block
-b
2b
kb
a (k+1)b
This Lecture
Quotient remainder theorem Greatest common divisor & Euclidean algorithm Linear combination and GCD, extended Euclidean algorithm Prime factorization and other applications
Common Divisors
c is a common divisor of a and b means c|a and c|b. gcd(a,b) ::= the greatest common divisor of a and b.
Say a=8, b=10, then 1,2 are common divisors, and gcd(8,10)=2. Say a=10, b=30, then 1,2,5,10 are common divisors, and gcd(10,30)=10. Say a=3, b=11, then the only common divisor is 1, and gcd(3,11)=1.
Lets say a>b. 1. If a=kb, then gcd(a,b)=b, and we are done. 2. Otherwise, by the Quotient-Remainder Theorem, a = qb + r for r>0.
a=12, b=8 => 12 = 8 + 4 a=21, b=9 => 21 = 2x9 + 3 a=99, b=27 => 99 = 3x27 + 18
Example 1
gcd(a,b) if b = 0, then answer = a. else write a = qb + r answer = gcd(b,r)
Example 2
gcd(a,b) if b = 0, then answer = a. else write a = qb + r answer = gcd(b,r)
Example 3
gcd(a,b) if b = 0, then answer = a. else write a = qb + r answer = gcd(b,r)
GCD(662, 414) = GCD(414, 248) = GCD(248, 166) = GCD(166, 82) = GCD(82, 2) = GCD(2, 0)
662 = 1x414 + 248 414 = 1x248 + 166 248 = 1x166 + 82 166 = 2x82 + 2 82 = 41x2 + 0 Return value: 2.
When r = 0: Then gcd(b, r) = gcd(b, 0) = b since every number divides 0. But a = qb so gcd(a, b) = b = gcd(b, r), and we are done.
Euclids algorithm: In two iterations, then b is decreased by half. (why?) Then the running time is about 2log2(b) iterations.
Exponentially faster!!
This Lecture
Quotient remainder theorem Greatest common divisor & Euclidean algorithm Linear combination and GCD, extended Euclidean algorithm Prime factorization and other applications
Smallest positive integer linear combination d is an integer linear combination of a and b if d=sa+tb for integers s,t. spc(a,b) = smallest positive integer linear combination of a and b
Example: a = 259, b=70 259 = 370 + 49 70 = 149 + 21 49 = a 3b 21 = 70 - 49 21 = b (a-3b) = -a+4b 49 = 221 + 7 7 = 49 - 221 7 = (a-3b) 2(-a+4b) = 3a 11b 21 = 73 + 0 done, gcd = 7
This Lecture
Quotient remainder theorem Greatest common divisor & Euclidean algorithm Linear combination and GCD, extended Euclidean algorithm Prime factorization and other applications
(1) we can now write down gcd(a,b) as some concrete equation, (i.e. gcd(a,b) = sa+tb for some integers s and t), and this allows us to reason about gcd(a,b) much easier. (2) If we can find integers s and t so that sa+tb=c, then we can conclude that gcd(a,b) <= c. In particular, if c=1, then we can conclude that gcd(a,b)=1.
Unique Factorization
Theorem: There is a unique factorization.
proof: suppose, by contradiction, that there are numbers with two different factorization. By the well-ordering principle, we choose the smallest such n >1: n = p1 p2 pk = q1 q2 qm Since n is smallest, we must have that pi { qj all i,j (Otherwise, we can obtain a smaller counterexample.) Since p1|n = q1 q2 qm, so by Cor., p1|qi for some i. contradiction!
By the Theorem, there exist s,t,u,v such that sa + tb = 1 ua + vc = 1 Multiplying, we have (sa + tb)(ua + vc) = 1 saua + savc + tbua + tbvc = 1 (sau + svc + tbu)a + (tv)bc = 1 By the Theorem, since spc(a,bc)=1, we have gcd(a,bc)=1
3 Gallon Jug
5 Gallon Jug
Quick Summary
Make sure you understand the Euclidean algorithm and the extended Euclidean algorithm. Also make sure to understand the relation between GCD and SPC. It is the basic of all the elementary number theory well see.