Greatest Common Divisor - Computer Science...

22
Greatest Common Divisor Jordi Cortadella Department of Computer Science

Transcript of Greatest Common Divisor - Computer Science...

Page 1: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Greatest Common Divisor

Jordi Cortadella

Department of Computer Science

Page 2: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Simplifying fractions

114

42=57

21=19

7

114

42=19 ∙ 6

7 ∙ 6

gcd(114, 42) = 6

gcd(19, 7) = 1

greatest

common

divisor

Introduction to Programming © Dept. CS, UPC 2

Page 3: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

The largest square tile

What is the largest square tile that can exactly cover a wh rectangle?

w = 60

h=24

44

90 tiles of side-length 4

Introduction to Programming © Dept. CS, UPC 3

Page 4: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

The largest square tile

What is the largest square tile that can exactly cover a wh rectangle?

w = 60

h=24

66

90 tiles of side-length 4

40 tiles of side-length 6

Introduction to Programming © Dept. CS, UPC 4

Page 5: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

The largest square tile

What is the largest square tile that can exactly cover a wh rectangle?

w = 60

h=24

90 tiles of side-length 4

40 tiles of side-length 6

10 tiles of side-length 12

1212

Introduction to Programming © Dept. CS, UPC 5

Page 6: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

The largest square tile

What is the largest square tile that can exactly cover a wh rectangle?

w = 60

h=24

1212

60 24 = (5 12) (2 12) = (5 2) (12 12)

gcd(60, 24)largest

tilenumber

of tiles

Introduction to Programming © Dept. CS, UPC 6

Page 7: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Euclid (300 B.C.)

A fragment of Euclid’s Elements

http://www.math.ubc.ca/~cass/Euclid/papyrus/tha.jpg

Introduction to Programming © Dept. CS, UPC 7

Page 8: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Euclidean algorithm for gcd

Observation:

if d divides 114 and 42, it also divides 114 42

114

𝑑−42

𝑑=72

𝑑

Therefore, all common divisors of 114 and 42are also divisors of 72.

gcd 114, 42 = gcd(72, 42)

Introduction to Programming © Dept. CS, UPC 8

Page 9: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Euclidean algorithm for gcd

Introduction to Programming © Dept. CS, UPC 9

1071

462

147

21

Page 10: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Euclidean algorithm for gcd

• Properties:

gcd(a, a) = a; gcd(a, 0) = a

If a b, then gcd(a, b) = gcd(a b, b)

• Example a b

114 42

72 42

30 42

30 12

18 12

6 12

6 6 gcd (114, 42)

Introduction to Programming © Dept. CS, UPC 10

Page 11: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Euclidean algorithm for gcd

// Pre: a > 0, b > 0

// Returns the greatest common divisor of a and b

int gcd(int a, int b) {

while (a != b) {

if (a > b) a = a – b;

else b = b – a;

}

return a;

}

Introduction to Programming © Dept. CS, UPC 11

Page 12: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Euclidean algorithm for gcditeration a b

0 10,000,001 154

1 9,999,847 154

2 9,999,693 154

3 9,999,539 154

… … 154

64,934 165 154

64,935 11 154

64,936 11 143

64,937 11 132

64,938 11 121

64,939 11 110

… 11 …

64,947 11 22

64,948 11 11

10000001 154

11 64935

Introduction to Programming © Dept. CS, UPC 12

Page 13: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Faster Euclidean algorithm for gcd

• Properties:

gcd(a, 0) = a

If b 0 then gcd(a, b) = gcd(a%b, b)

• Example

a b

10,000,001 154

11 154

11 0

Introduction to Programming © Dept. CS, UPC 13

Page 14: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

// Pre: a 0, b 0

// Returns the greatest common divisor of a and b

int gcd(int a, int b) {

while (a != 0 and b != 0) {

if (a > b) a = a%b;

else b = b%a;

}

return a + b;

}

Faster Euclidean algorithm for gcd

Termination: a == 0 or b == 0

Introduction to Programming © Dept. CS, UPC 14

Page 15: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

a b

42 114

114 42

42 30

30 12

12 6

6 0

Faster Euclidean algorithm for gcd

// Pre: a 0, b 0

// Returns the greatest common divisor of a and b

int gcd(int a, int b) {

while (b > 0) {

int r = a%b;a = b;b = r;

}

return a;

}

yx

y x%y>

a b

Introduction to Programming © Dept. CS, UPC 15

Page 16: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Comparing algorithms for gcd

int gcd(int a, int b) {

while (b > 0) {

int r = a%b;a = b;b = r;

}

return a;

}

int gcd(int a, int b) {

while (a > 0 and b > 0) {

if (a > b) a = a%b;

else b = b%a;

}

return a + b;

}

Every iteration:

• 3 comparisons

• 1 mod operation

Every iteration:

• 1 comparison

• 1 mod operation

Introduction to Programming © Dept. CS, UPC 16

Page 17: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Efficiency of the Euclidean algorithm

How many iterations will Euclid’s algorithm need to calculate gcd(a,b) in the worst case (assume a > b)?

– Subtraction version: a iterations(consider gcd(1000000, 1))

– Modulo version: 5d(b) iterations,where d(b) is the number of digits of b(Gabriel Lamé, 1844)

Introduction to Programming © Dept. CS, UPC 17

Page 18: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Binary Euclidean algorithm

Computers can perform 2 and /2 operations efficiently

(217)10 = (11011001)2

(2172)10 = (434)10 = (110110010)2

(217/2)10 = (108)10 = (1101100)2

(217%2)10 = 1 (least significant bit)

Introduction to Programming © Dept. CS, UPC 18

Page 19: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Binary Euclidean algorithm

Assume a b:

a b gcd(a,b)

a 0 a

even even 2gcd(a/2, b/2)

even odd gcd(a/2, b)

odd even gcd(a,b/2)

odd odd gcd((a-b)/2, b)

a b

132 84

66 42

33 21

6 21

3 21

3 9

3 3

3 0

2

2

3

12

Introduction to Programming © Dept. CS, UPC 19

Page 20: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

// Pre: a 0, b 0// Returns the greatest common divisor of a and b

int gcd(int a, int b) {int r = 1; // Accumulates common powers of twowhile (a != 0 and b != 0) {

if (a%2 == 0 and b%2 == 0) {a = a/2;b = b/2;r = r2;

}else if (a%2 == 0) a = a/2;else if (b%2 == 0) b = b/2;else if (a > b) a = (a - b)/2;else b = (b – a)/2;

}return (a + b)r;

}

Binary Euclidean algorithm

Introduction to Programming © Dept. CS, UPC 20

Page 21: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

// Pre: a 0, b 0// Returns the greatest common divisor of a and b

int gcd(int a, int b) {if (a == 0 or b == 0) return a + b;

int r = 1; // Accumulates common powers of twowhile (a%2 == 0 and b%2 == 0) {

a = a/2;b = b/2;r = r2;

}

while (a != b) {if (a%2 == 0) a = a/2;else if (b%2 == 0) b = b/2;else if (a > b) a = (a - b)/2;else b = (b – a)/2;

}return ar;

}

Binary Euclidean algorithm

Introduction to Programming © Dept. CS, UPC 21

Page 22: Greatest Common Divisor - Computer Science Departmentjordicf/Teaching/FME/Informatica/pdf/Gcd.pdf · The largest square tile What is the largest square tile that can exactly cover

Summary

• Euclid’s algorithm is very simple.

• It is widely used in some applications related to cryptography (e.g., electronic commerce).

• Euclid’s algorithm efficiently computes thegcd of very large numbers.

• Question: how would you compute the least common multiple of two numbers efficiently?

Introduction to Programming © Dept. CS, UPC 22