Computing GCDs for b 0 The Euclidean Proof: a = qb + r Algorithm - - PowerPoint PPT Presentation

computing gcd s
SMART_READER_LITE
LIVE PREVIEW

Computing GCDs for b 0 The Euclidean Proof: a = qb + r Algorithm - - PowerPoint PPT Presentation

GCD Remainder Lemma Mathematics for Computer Science Lemma: MIT 6.042J/18.062J gcd(a,b) = gcd(b, rem(a,b)) Computing GCDs for b 0 The Euclidean Proof: a = qb + r Algorithm any divisor of 2 of these terms must divide all 3. Albert R


slide-1
SLIDE 1

Albert R Meyer March 6, 2015

gcdeuclid.1

Computing GCD’s The Euclidean Algorithm

Mathematics for Computer Science

MIT 6.042J/18.062J

Albert R Meyer March 6, 2015

Proof: a = qb + r

any divisor of 2 of these terms must divide all 3.

Lemma:

gcd(a,b) = gcd(b, rem(a,b))

for b ≠ 0

GCD Remainder Lemma

gcdeuclid.2

Albert R Meyer March 6, 2015

Proof: a = qb + r

so a,b and b,r have the same divisors

GCD Remainder Lemma

gcdeuclid.3

Lemma:

gcd(a,b) = gcd(b, rem(a,b))

for b ≠ 0

Albert R Meyer March 6, 2015

Example: a = 899, b=493 GCD(899, 493) = GCD(493, 406) = GCD(406, 87) = GCD(87, 58) = GCD(58, 29) = GCD(29, 0) = 29

gcdeuclid.4

GCD example

1

slide-2
SLIDE 2

Albert R Meyer March 6, 2015

Euclidean Algorithm

as a State Machine: States ::= start ::= (a,b) state transitions defined by

(x,y) → (y, rem(x,y))

for y ≠ 0

gcdeuclid.5

N× N

Albert R Meyer March 6, 2015

By Lemma, gcd(x,y) is constant. so preserved invariant is P((x,y)) ::= [gcd(a,b) = gcd(x,y)]

P(start) is trivially true: [gcd(a,b) = gcd(a,b)]

gcdeuclid.6

GCD partial correctness

Albert R Meyer March 6, 2015

GCD partial correctness

at termination (if any)

x = gcd(a,b)

Proof: at termination, y = 0, so x = gcd(x,0) = gcd(x,y) = gcd(a,b) preserved invariant

gcdeuclid.7

Albert R Meyer March 6, 2015

GCD Termination

At each transition, x is replaced by y.

gcdeuclid.8

2

slide-3
SLIDE 3

Albert R Meyer March 6, 2015

GCD Termination

At each transition, x is replaced by y. If y < x/2, then x gets halved at this step.

gcdeuclid.9

Albert R Meyer March 6, 2015

GCD Termination

At each transition, x is replaced by y. If y < x/2, then x gets halved at this step. If y > x/2, then rem(x,y) = x – y < x/2, so y gets halved when it is replaced by rem(x,y) after the next step.

gcdeuclid.10

Albert R Meyer March 6, 2015

GCD Termination

y halves or smaller at every other step, so reaches minimum in ≤ 2 log2 b steps.

gcdeuclid.11

3

slide-4
SLIDE 4

MIT OpenCourseWare https://ocw.mit.edu

6.042J / 18.062J Mathematics for Computer Science

Spring 2015 For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.