Euclidean Algorithm

27
Euclidean Algorithm Applied Symbolic Computation CS 567 Jeremy Johnson

description

Euclidean Algorithm. Applied Symbolic Computation CS 567 Jeremy Johnson. Greatest Common Divisors. g = gcd ( a,b ) g |a and g|b e|a and e|b  e|g. Unique Factorization. p |ab  p|a or p|b a = p 1    p t = q 1    q s  s = t and  i j: p i = q j - PowerPoint PPT Presentation

Transcript of Euclidean Algorithm

Page 1: Euclidean Algorithm

Euclidean Algorithm

Applied Symbolic ComputationCS 567

Jeremy Johnson

Page 2: Euclidean Algorithm

Greatest Common Divisors

• g = gcd(a,b) – g|a and g|b– e|a and e|b e|g

Page 3: Euclidean Algorithm

Unique Factorization

• p|ab p|a or p|b

• a = p1 pt = q1 qs s = t and i j: pi= qj

• a = p1e1 pt

et

• b = p1f1 pt

ft

• gcd(a,b) = p1min(e1,f1) pt

min(et,ft)

Page 4: Euclidean Algorithm

Bezout’s Identity

• g = gcd(a,b) • x,y: g = ax + by

Page 5: Euclidean Algorithm

Bezout’s Identity

• g = gcd(a,b) • x,y: g = ax + by

Page 6: Euclidean Algorithm

Euclidean Algorithm

g = gcd(a,b) if (b = 0) then return a; else return gcd(b,a mod b)

Page 7: Euclidean Algorithm

Correctness

Page 8: Euclidean Algorithm

Tail Recursion

g = gcd(a,b) if (b = 0) then return a; else return gcd(b,a mod b)

Page 9: Euclidean Algorithm

Iterative Algorithm

g = gcd(a,b) a1 = a; a2 = b; while (a2 0) a3 = a1 mod a2; a1 = a2; a2 = a3; } return a1;

Page 10: Euclidean Algorithm

Remainder Sequence

a1 = a, a2 = ba1 = q3 a2 + a3, 0 a3 < a2

ai = qi ai+1 + ai+2, 0 ai+2 < ai+1

an= qn an+1

gcd(a,b) = an+1

Page 11: Euclidean Algorithm

Bounding Number of Divisions

Theorem. Let a b 0 and n = number of divisions required by the Euclidean algorithm to compute gcd(a,b). Then n < 2lg(a).

Page 12: Euclidean Algorithm

Bounding Number of Divisions

Page 13: Euclidean Algorithm

Fibonacci Numbers

• F0 = 0, F1 = 1

• Fn+2 = Fn+1 + Fn

Page 14: Euclidean Algorithm

Solving the Fibonacci Recurrence

• Fn = 1/5(n + * n), = (1 + 5)/2, * = (1 - 5)/2• Fn 1/5n+1

Page 15: Euclidean Algorithm

Solving the Fibonacci Recurrence

Page 16: Euclidean Algorithm

Solving the Fibonacci Recurrence

Page 17: Euclidean Algorithm

Maximum Number of Divisions

Theorem. The smallest pair of integers that require n divisions to compute their gcd is Fn+2 and Fn+1.

Page 18: Euclidean Algorithm

Maximum Number of Divisions

Theorem. Let a b 0 and n = number of divisions required by the Euclidean algorithm to compute gcd(a,b). Then n < 1.44 lg(a).

Page 19: Euclidean Algorithm

Maximum Number of Divisions

Page 20: Euclidean Algorithm

Extended Euclidean Algorithm

g = gcd(a,b,*x,*y) a1 = a; a2 = b; x1 = 1; x2 = 0; y1 = 0; y2 = 1; while (a2 0) a3 = a1 mod a2; q = floor(a1/a2); x3 = x1 – q*x2; y3 = y1 – q*y2; a1 = a2; a2 = a3; x1 = x2; x2 = x3; y1 = y2; y2 = y3; } return a1;

Page 21: Euclidean Algorithm

Correctness

Page 22: Euclidean Algorithm

Correctness

Page 23: Euclidean Algorithm

Probability of Relative Primality

p/d2 = 1 p = 1/(2)

(z) = 1/nz

(2) = 2/6

Page 24: Euclidean Algorithm

Formal Proof

Let qn be the number of 1 a,b n such that gcd(a,b) = 1. Then limn qn/n2 = 6/2

Page 25: Euclidean Algorithm

Mobius Function

• (1) = 1• (p1 pt) = -1t

• (n) = 0 if p2|n

(ab) = (a)(b) if gcd(a,b) = 1.

Page 26: Euclidean Algorithm

Mobius Inversion

d|n (d) = 0(n (n)ns)(n 1/ns) = 1

Page 27: Euclidean Algorithm

Formal Proof

qn = n n/k2

limn qn/n2 = n (n)n2 = n 1/n2 = 6/2