Top Banner
Greatest Common Divisor Jordi Cortadella Department of Computer Science
22

Greatest Common Divisor Jordi Cortadella Department of Computer Science.

Jan 03, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

Greatest Common Divisor

Jordi CortadellaDepartment of Computer Science

Page 2: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

Simplifying fractions

(114, 42) = 6

(19, 7) = 1

greatestcommondivisor

Introduction to Programming © Dept. CS, UPC 2

Page 3: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

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 Jordi Cortadella Department of Computer Science.

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 440 tiles of side-length 6

Introduction to Programming © Dept. CS, UPC 4

Page 5: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

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 440 tiles of side-length 610 tiles of side-length 12

1212

Introduction to Programming © Dept. CS, UPC 5

Page 6: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

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

tilenumberof tiles

Introduction to Programming © Dept. CS, UPC 6

Page 7: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

Euclid (300 B.C.)

A fragment of Euclid’s Elementshttp://www.math.ubc.ca/~cass/Euclid/papyrus/tha.jpg

Introduction to Programming © Dept. CS, UPC 7

Page 8: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

Euclidean algorithm for gcdObservation: if d divides 114 and 42, it also divides 114 42

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

Introduction to Programming © Dept. CS, UPC 8

Page 9: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

Euclidean algorithm for gcd

Introduction to Programming © Dept. CS, UPC 9

1071

462

147

21

Page 10: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

Euclidean algorithm for gcd• Properties:

gcd(a, a) = a; gcd(a, 0) = aIf a b, then gcd(a, b) = gcd(a b, b)

• Example: a b114 42

72 4230 4230 1218 12

6 126 6

gcd (114, 42)

Introduction to Programming © Dept. CS, UPC 10

Page 11: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

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 Jordi Cortadella Department of Computer Science.

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 Jordi Cortadella Department of Computer Science.

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 15411 15411 0

Introduction to Programming © Dept. CS, UPC 13

Page 14: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

// 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

not

Introduction to Programming © Dept. CS, UPC 14

Page 15: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

a b42 114

114 4242 3030 1212 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 Jordi Cortadella Department of Computer Science.

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 Jordi Cortadella Department of Computer Science.

Efficiency of the Euclidean algorithmHow 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 Jordi Cortadella Department of Computer Science.

Binary Euclidean algorithmComputers 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 Jordi Cortadella Department of Computer Science.

Binary Euclidean algorithmAssume 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 b132 84

66 4233 21

6 213 213 93 33 0

2

2

3

12

Introduction to Programming © Dept. CS, UPC 19

Page 20: Greatest Common Divisor Jordi Cortadella Department of Computer Science.

// 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 two while (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 Jordi Cortadella Department of Computer Science.

// 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 two while (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 Jordi Cortadella Department of Computer Science.

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