Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.

Similar presentations


Presentation on theme: "CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett."— Presentation transcript:

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

15

16 The value of a keeps decreasing, which proves termination

17 Euclid’s algorithm: correctness g=gcd(a,b)

18 Euclid’s algorithm: correctness

19

20

21

22 g=gcd(a,b) Proved!

23 Euclid’s algorithm: speed How many iterations?

24 Euclid’s algorithm: speed

25

26

27

28 3. Extended Euclid’s algorithm Using algorithms to do math! 28

29 Extended Euclid’s algorithm

30

31


Download ppt "CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett."

Similar presentations


Ads by Google