Greatest Common Divisor (gcd)

advertisement
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Greatest Common Divisor (gcd)
Greatest Common Divisor (gcd).
The Greatest Common Divisor (gcd ) is also known as the
highest common factor (hcf ).
Example:
A fraction is in its lowest form ba , when gcd (a, b) = 1, i.e. the
greatest common divisor is 1.
Example:
gcd (16, 24) = 8 as 8 is the greatest common divisor (highest
common factor) of 16 and 24.
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
gcd definition
gcd(a,b)
The positive integer, g , is the gcd (greatest common divisor) of
integers a and b
i.e. g = gcd (a, b) iff
1) g |a and g |b i.e. g is a common divisor of a and b
2) If h|a and h|b then h ≤ g i.e. g is the greatest common divisor.
Relatively Prime
If gcd (a, b) = 1 then a and b are relatively prime.
i.e. a and b have no non-trivial (i.e. 6=1 or 6= −1) common factors.
e.g. 4 and 9 are relatively prime.
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Finding gcd(a,b)
For a ≥ 0 ∧ b > 0, we have from Euclid’s Remainder Theorem
(some unique q and r ):
a =b∗q+r ∧ 0≤r <b
Let g = gcd (a, b), then g |a and g |b and so g |a − b ∗ q
tf: since r = a − b ∗ q then g |r .
Show g = gcd (b, r )
Pf:
1) g |b and also g |r
2) Let h|b and h|r , Show h ≤ g .
Pf: Assume h > g , then since h|b and h|r
h|(b ∗ q + r ) but a = b ∗ q + r tf. h|a
but then h|a and h|b and so h is a divisor of a and b greater than
g , contradicting that g is the greatest divisor of a and b.
Greatest Common Divisor (gcd)
GCD Properties
Properties of gcd
1.
2.
3.
4.
gcd (a, b) = gcd (b, a mod b).
gcd (a, b) = gcd (b, a)
gcd (k ∗ b, b) = b
gcd (a, 0) = a as a|a and a|0.
Example: Find gcd (72, 15).
gcd (72, 15)
= gcd (15, 72 mod 15)
= gcd (15, 12)
= gcd (12, 3)
= 3, as 3|12.
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
a*x+b*y=1
Question:
Given a 5 litre jar and a 13 litre jar can we get exactly 1 litre in one
of them by filling and refilling the jars from a bigger container.
Can we find integers x and y such that
5 ∗ x + 13 ∗ y = 1
Solution:
Let x = −5 and y = 2 to get
5 ∗ (−5) + 13 ∗ 2 = 1
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Question:
Can we find integers x and y such that
6 ∗ x + 14 ∗ y = 1
Solution:
No Solution!
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
a*x+b*y=gcd(a,b)
In general, we can find integers x and y such that:
a ∗ x + b ∗ y = gcd (a, b)
In particular, if gcd (a, b) = 1 , i.e. a and b are relatively prime,
then we can find we can find integers x and y such that:
a∗x +b∗y =1
An equation such as a ∗ x + b ∗ y = g is a linear Diophantine
Equation.
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Proof a*x+b*y=gcd(a,b)
Theorem
Given a, b, with b ≥ 0, show there exists x and y such that
a ∗ x + b ∗ y = gcd (a, b)
Proof (by induction on b)
Base case: b = 0.
Then a ∗ 1 + b ∗ 0 = gcd (a, b) as gcd (a, 0) = a.
Induction step: (Assume true for k < b, show true for b)
Since a mod b < b then there is x 0 and y 0 such
b ∗ x 0 + (a mod b) ∗ y 0 = gcd (b, a mod b)
but gcd (b, a mod b) = gcd (a, b)
Also, a mod b = a − b ∗ (a div b)
tf.
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
gcd (a, b)
= gcd (b, a mod b)
= b ∗ x 0 + (a mod b) ∗ y 0
= b ∗ x 0 + (a − b ∗ (a div b)) ∗ y 0
= b ∗ x 0 + a ∗ y 0 − b ∗ (a div b) ∗ y ’
= a ∗ y 0 + b ∗ (x 0 − (a div b) ∗ y 0 )
= a ∗ x + b ∗ y where x = y 0 and y = (x 0 − (adivb) ∗ y 0 )
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Alternate definition gcd
Since there are integers x, y such that
gcd (a, b) = a ∗ x + b ∗ y
then if h|a and h|b then h|(a ∗ x + b ∗ y )
tf. h|gcd (a, b).
Alternative definition of gcd(a,b)
Definition
g = gcd (a, b) iff
1) g |a and g |b i.e. g is a common divisor of a and b
2) If h|a and h|b then h|g i.e. any common divisor divides g .
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Construct Solution to a*x+b*y=gcd(a,b)
To construct a solution of a ∗ x + b ∗ y = gcd (a, b), find gcd (a, b)
via Euclid’s Algorithm; then ’reverse’ the calculation to find x and
y.
Example
Find integers x, y such that
1147 ∗ x + 851 ∗ y = gcd (1147, 851)
Using Euclid’s Remainder Theorem:
1147 = 851 ∗ 1 + 296
tf. gcd (1147, 851) = gcd (851, 296)
851 = 296 ∗ 2 + 259
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Cont’d
296 = 259 ∗ 1 + 37
tf. gcd (1147, 851) = gcd (259, 37)
{ 259 = 7 ∗ 37 }
tf.
gcd (1147, 851) = 37
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Find x,y
Then ’reversing’ the calculation (Euclid’s Algorithm):
37 = 296 ∗ 1 − 259 ∗ 1
= 296 ∗ 1 − (851 − 296 ∗ 2)
= 851 ∗ (−1) + 296 ∗ 3
= 851 ∗ (−1) + (1147 − 851 ∗ 1) ∗ 3
= 1147 ∗ 3 + 851 ∗ (−1) + 851 ∗ (−3)
= 1147 ∗ 3 + 851 ∗ (−4)
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
tf.
37 = 1147 ∗ 3 + 851 ∗ (−4)
Solution:
37 = 1147 ∗ x + 851 ∗ y
where x = 3 and y = −4
Check by calculation:
1147 ∗ 3 − 851 ∗ 4 = 3441 − 3404 = 37
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Exercise
Exercise:
Find x, y such that
1785 ∗ x + 374 ∗ y = gcd (1785, 374)
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Euclid’s Lemma
Theorem
Euclid’s Lemma
If gcd (a, b) = 1 i.e. a and b are relatively prime and also a|(b ∗ c)
then a|c
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Proof Euclid’s Lemma
Proof.
Since gcd (a, b) = 1 there exists x and y such that a ∗ x + b ∗ y = 1
tf. c ∗ a ∗ x + c ∗ b ∗ y = c.
From assumption that a|(b ∗ c) we have a|c ∗ b ∗ y .
Also a|c ∗ a ∗ x ,
tf. a|c.
Corollary 1
If p is a prime and p|an then p|a .
Corollary 2
If a ∗ b ≡n a ∗ c and a and n are relatively prime, then b ≡n c .
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Binomial Coefficient
Binomial Coefficient or ’Choose r from n’
n
r
=
=
n ∗ (n − 1) ∗ · · · ∗ (n − r + 1)
r!
n!
(n − r )!r !
By convention:
0! =1 n
n
tf.
= 1 and
=1
0
n
Greatest Common Divisor (gcd)
Properties
n
r
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
n
Properties
r
Binomial Thm:
n
n
n
n
n
n−1
(x + 1) =
x +
x
+ ··· +
x n−r +
0
1
r
n
n
1
··· +
x +
x0
n−1
n
p
If p is prime and 0 < r < p then p|
.
r
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Lemma gcd(r !, p) = 1, for prime p
Assume p is prime and 0 < r < p
Show gcd (r !, p) = 1.
Since p is prime, only divisors/factors of p are 1 and p.
Assume gcd (r !, p) 6= 1 then gcd (r !, p) = p.
tf. p|r !
i.e. p|r ∗ (r − 1)!
Since 1 < r < p and p is prime, gcd (r , p) = 1,
tf. From Euclid’s Lemma: (If gcd (a, b) = 1 and a|(b ∗ c) then a|c .)
p|(r − 1)! ,
Similary, p|(r − 2)! as gcd (r − 1, p) = 1
Continuing, we eventually get p|1 , a contradiction.
Greatest Common Divisor (gcd)
p|
p
r
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
when p is prime
Show if p is prime and 0 < r < p then p|
Proof:
If r = 1 then
p
r
= p tf. p|
p
r
p
r
Case
1<r <p
p
+1)
= p∗(p−1)∗···∗(p−r
r
!
r
+1)
Since p∗(p−1)∗···∗(p−r
∈Z
r!
p∗(p−1)∗···∗(p−r +1)
i.e.
= k , for some k ∈ Z,
r!
then r ! | p ∗ (p − 1) ∗ · · · ∗ (p − r + 1) .
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
r ! | p ∗ (p − 1) ∗ · · · ∗ (p − r + 1) and gcd (r !, p) = 1
{Euclid’s Lemma: (If gcd (a, b) = 1 and a|(b ∗ c) then a|c .)}
+1)
r ! | (p − 1) ∗ · · · ∗ (p − r + 1) i.e. (p−1)∗···∗(p−r
= j, some j ∈ Z
r
!
p
p
+1)
i.e.
=p∗j
hence:
= p ∗ (p−1)∗···∗(p−r
r!
r
r
p
and so p|
.
r
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Fermat’s Little Thm:
The version of Fermat’s Little Theorem we prove is:
Fermat’s Little Theorem
For a > 0 and a prime p
ap ≡p a
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
ap ≡p a
Proof is by induction on a
Base Case: a = 1
Clear that 1p ≡p 1
Induction Step:
Assume true for a, show true for a + 1.
By binomial Thm:
p
(a + 1)
=
p
p
p
p
p−1
p−r
a +
a
+ ··· +
a
+ ··· +
a+1 .
1
r
p−1
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Proof cont’d
p
p
p
Since
,...
,...
are divisible by p,
1
r
p−1
p
i.e. p|
for k ∈ {1..p − 1} .
k
tf. (a + 1)p ≡p ap + 0 + . . . 0 · · · + 0 + 1
i.e. (a + 1)p ≡p ap + 1
Since by induction ap ≡p a
tf (a + 1)p ≡p a + 1 .
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Corollary: Fermat’s Little Thm.
Corollary: Fermat’s Little Theorem
For integer a > 0 and a prime, p, if p 6 |a, i.e. a is not a multiple of
p, then
ap−1 ≡p 1
Assume a > 0 and p is prime
Show ap ≡p a iff p 6 |a ⇒ (ap−1 ≡p 1)
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Corollary: Fermat’s Little Thm. (Cont’d)
LHS ⇒ RHS
Given ap ≡p a , show p 6 |a ⇒ (ap−1 ≡p 1)
Assume p 6 |a
We have ap ≡p a
tf. p|ap − a
tf. p|(ap−1 − 1) ∗ a
but by assumption, p 6 |a
tf. p|(ap−1 − 1)
i.e. (ap−1 ≡p 1)
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Corollary: Fermat’s Little Thm. (Cont’d)
RHS ⇒ LHS
p 6 |a ⇒ (ap−1 ≡p 1) , show ap ≡p a, for all a > 0
Case p|a
tf. p|(a ∗ (ap−1 − 1))
i.e. p|(ap − a)
i.e. ap ≡p a
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Corollary: Fermat’s Little Thm. (Cont’d)
Case p 6 |a
tf. from assumption,
p|(ap−1 − 1)
tf. p|(ap−1 − 1) ∗ a
i.e. p|(ap − a)
i.e. ap ≡p a
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Application Fermat’s Little Thm.
Example:
Let p = 11 and a = 3 then 310 ≡11 1 i.e. 310 mod 11 = 1.
Check via the following table:
k
3k mod 11
0
1
1
3
2
9
3
5
4
34 ≡11 5 ∗ 3 ≡11 4
5
1
6
3
7
9
8
5
9
4
Double Check: 310 = 59049 = 11 ∗ 5368 + 1.
Also 311 ≡11 3 . Since 310 ≡11 1 then multiplying both sides by 3
we get: 311 ≡11 3.
Double check: 311 = 177147 = 11 ∗ 16104 + 3.
10
1
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Pidgeon-Hole Principle
Pidgeon-Hole Principle
If m items are put into n boxes and m > n, then some boxes have
more than one item. Since m > n, after filling up the n boxes we
have still items left which means some box has more than 1 item.
e.g. In a crowd of 367 people, at least two people have the same
birthday.
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
When gcd(a,n)=1, ak ≡n 1 for some k ≤ n
Theorem
If a is relatively prime to n then there exists k > 0 such that k ≤ n
and ak ≡n 1 .
Proof
Assume a is relatively prime to n. Consider the remainders of
a, a2 , . . . , an+1
when divided by n. There are n + 1 elements in {a, a2 , . . . , an+1 }
and there can be only n remainders (from 0 to n − 1). By the
Pidgeon-Hole Principle, there exists i, j ∈ {1..n + 1) and i 6= j
(assume i < j) such that ai and aj have the same remainder, i.e.
ai ≡n aj .
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Cont’d
Since i < j , j − i > 0 and j − i ≤ n (since j is at most n + 1 and i
is at least 1). Since a is relatively prime to n (by Corollary 2 above:
If a ∗ b ≡n a ∗ c and a and n are relatively prime, then b ≡n c .),
ai−1 ≡n aj−1 . Continuing to cancel a0 s on both sides we get (since
i < j), 1 ≡n aj−i i.e. ak ≡n 1 where k = j − i.
Example
Let a = 3 and n = 10 then 3 and 10 are relatively prime. Find a k
such that 3k ≡10 1 . In this case, 34 = 81 and so 34 ≡10 1.
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Finding inverse using ak ≡n 1
Finding inverse
Let a ∈ Zn . If a and n are relatively prime, there exists a k such
that 0 < k ≤ n and ak ≡n 1.
Since ak = a ∗ ak−1 then a ∗n ak−1 ≡n 1 and so ak−1 is the inverse
of a.
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Least Common Multiple, lcm
In the current context, read x|y as ’y is a multiple of x’
Definition
l = lcm(a, b) iff (l > 0)
1. a|l and b|l i.e. l is a common multiple of of a and b
2. If a|m and b|m then l ≤ m. i.e. l is the least common multiple
Alternative Definition:
Definition
l = lcm(a, b) iff (l > 0)
1. a|l and b|l i.e. l is a common multiple of of a and b
2. If a|m and b|m then l |m. i.e. any common multiple of a and b
is a multiple of l .
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Calculating lcm(a,b)
Definition
lcm(x, y ) =
x ∗y
gcd (x, y )
Example
Find lcm(54,12).
gcd (54, 12) = gcd (12, 6) = 6
tf.
54 ∗ 12
12
lcm(54, 12) =
= 54 ∗
= 54 ∗ 2 = 108
6
6
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Factorisation Theorem
A positive integer, n, can be factorised uniquely into powers of
primes.
n=
∞
Y
piαi
i=1
where pi is the i th prime and p1 < p2 < . . .
primes = 2, 3, 5, 7, 11, 13, . . .
12250 = 21 ∗ 30 ∗ 53 ∗ 72 ∗ 110 . . .
∞
Y
1
0
3
2
= 2 ∗3 ∗5 ∗7 ∗
pi0
i=5
Exercise: Find the prime factors of 10101.
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Finding gcd and lcm
Let a =
Q∞
αi
i=1 pi
and b =
Q∞
βi
i=1 pi
gcd (a, b) =
∞
Y
then
min(αi ,βi )
pi
i=1
and
lcm(a, b) =
∞
Y
i=1
max(αi ,βi )
pi
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
Cont’d
Example
Find gcd(54,12) and lcm(54,12)
54 = 21 ∗ 33 and 12 = 22 ∗ 31
gcd (54, 12) = 2min(1,2) ∗ 3min(3.1)
= 2∗3
= 6
Also
lcm(54, 12) = 2max(1,2) ∗ 3max(3.1)
= 22 ∗ 33
= 4 ∗ 27
= 108
Greatest Common Divisor (gcd)
GCD/HCF
Diophantine Equation a*x+b*y=1
Binomial Coefficient
Fermat’s Little Theorem
Lowest/Least Common Multiple
Factorisation Theorem
gcd and lcm using Factorisation Thm.
Calculating gcd and lcm using the Factorisation Theorem is not
efficient.
Consider gcd (1147, 851).
851 = 23 ∗ 37 = 231 ∗ 310 ∗ 371
1147 = 31 ∗ 37 = 230 ∗ 311 ∗ 371
gcd (1147, 851) = 23min(0,1) ∗ 31min(1,0) ∗ 37min(1,1)
= 37
lcm(1147, 851) = 23max(0,1) ∗ 31max(1,0) ∗ 37max(1,1)
= 26381
Download