Greatest Common Divisor: 3 Gallon Jug 5 Gallon Jug
Greatest Common Divisor: 3 Gallon Jug 5 Gallon Jug
Greatest Common Divisor: 3 Gallon Jug 5 Gallon Jug
Dec 28
This Lecture
Given any b, we can divide the integers into many blocks of b numbers.
a
-b b 2b kb (k+1)b
0
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.
a = qb + r
gcd(a,b)
if b = 0, then answer = a.
else
write a = qb + r
answer = gcd(b,r)
Example 1
gcd(a,b)
if b = 0, then answer = a.
else
write a = qb + r
answer = gcd(b,r)
gcd(a,b)
if b = 0, then answer = a.
else
write a = qb + r
answer = gcd(b,r)
gcd(a,b)
if b = 0, then answer = a.
else
write a = qb + r
answer = gcd(b,r)
a = qb + r
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.
Correctness of Euclid’s GCD Algorithm
When r > 0:
Euclid’s algorithm:
Exponentially faster!!
This Lecture
Proof of (3)
d | a => a = dk1
d | b => b = dk2
Similarly, spa(a,b) | b.
So, spc(a,b) is a common divisor of a and b, thus by definition spc(a,b) <= gcd(a,b).
Extended GCD Algorithm
259 = 3·70 + 49 49 = a – 3b
70 = 1·49 + 21 21 = 70 - 49
21 = b – (a-3b) = -a+4b
49 = 2·21 + 7 7 = 49 - 2·21
= b – (a-b) = -a + 2b
= (a-b) – 4(-a+2b) = 5a - 9b
87 = 1·58 + 29 so 29 = 87 – 1·58
p0 ≤ p1 ≤ ··· ≤ pk
p0 p1 ··· pk = n
Example:
61394323221 = 3·3·3·7·11·11·37·37·37·53
Unique Factorization
n = p1·p2···pk = q1·q2···qm
sa + tb = 1
ua + vc = 1
Simon says: On the fountain, there should be 2 jugs, do you see them? A
5-gallon and a 3-gallon. Fill one of the jugs with exactly 4 gallons of
water and place it on the scale and the timer will stop. You must be
precise; one ounce more or less will result in detonation. If you're still
alive in 5 minutes, we'll speak.
Die Hard
Bruce: Wait, wait a second. I don’t get it. Do you get it?
Samuel: No.
Bruce: Get the jugs. Obviously, we can’t fill the 3-gallon jug with 4
gallons of water.
Samuel: Obviously.
Bruce: All right. I know, here we go. We fill the 3-gallon jug exactly to the
top, right?
Samuel: Uh-huh.
Bruce: Okay, now we pour this 3 gallons into the 5-gallon jug, giving us
exactly 3 gallons in the 5-gallon jug, right?
Samuel: Right, then what?
Bruce: All right. We take the 3-gallon jug and fill it a third of the way...
Samuel: No! He said, “Be precise.” Exactly 4 gallons.
Bruce: Sh - -. Every cop within 50 miles is running his a - - off and I’m out
here playing kids games in the park.
Die Hard
Supplies:
3 Gallon Jug
9 Gallon Jug
Water
Invariant Method
Bruce Dies!
Generalized Die Hard
Given jug of 3 and jug of 9, is it possible to have exactly 4 gallons in one jug?
Given jug of 21 and jug of 26, is it possible to have exactly 3 gallons in one jug?
Given jug of 21 and jug of 26, is it possible to have exactly 3 gallons in one jug?
gcd(21,26) = 1
Þ 5x21 – 4x26 = 1
Þ 15x21 – 12x26 = 3
Repeat 15 times:
1. Fill the 21-gallon jug.
2. Pour all the water in the 21-gallon jug into the 26-gallon jug.
Whenever the 26-gallon jug becomes full, empty it out.
General Solution for Die Hard
15x21 – 12x26 = 3
Repeat 15 times:
1. Fill the 21-gallon jug.
2. Pour all the water in the 21-gallon jug into the 26-gallon jug.
Whenever the 26-gallon jug becomes full, empty it out.
Given two jugs with capacity A and B with A < B, the target is C.
Repeat s times:
1. Fill the A-gallon jug.
2. Pour all the water in the A-gallon jug into the B-gallon jug.
Whenever the B-gallon jug becomes full, empty it out.