Download presentation
Presentation is loading. Please wait.
Published byHugo Hopkins Modified over 8 years ago
1
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
2
Today’s Topics: 1. GCD 2. Euclid’s algorithm 3. Extended Euclid’s algorithm 2
3
1. GCD The poor man version of prime factorization 3
4
GCD Greatest common divisor Given two positive integers a,b, their GCD is the largest integer n such that n|a and n|b 4
5
GCD What is GCD(20,30)? A. 5 B. 10 C. 20 D. 30 E. Other 5
6
GCD What is GCD(101281371,347832984723)? 6
7
GCD How can we compute GCD(a,b)? Simple way: (i) Compute prime factorization of a,b (ii) Take common primes and prime powers Example: if a=2 10 3 8 5 9 and b=2 17 3 5 then GCD(a,b)=2 10 3 5 However, we believe that computing the prime factorization of large numbers is hard… Euclid’s algorithm provides a much faster way 7
8
2. Euclid’s algorithm Fast GCD 8
9
Euclid’s algorithm
10
(a,b) (b,a mod b)
11
Euclid’s algorithm
12
a 20 30 20 10 b 30 20 10 0 Example run: a=20, b=30
13
Euclid’s algorithm The same basic questions 1. Does it always terminate? 2. Does it return the correct answer? 3. How fast is it?
14
Euclid’s algorithm: termination
16
The value of a keeps decreasing, which proves termination
17
Euclid’s algorithm: correctness g=gcd(a,b)
18
Euclid’s algorithm: correctness
22
g=gcd(a,b) Proved!
23
Euclid’s algorithm: speed How many iterations?
24
Euclid’s algorithm: speed
28
3. Extended Euclid’s algorithm Using algorithms to do math! 28
29
Extended Euclid’s algorithm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.