Docsity

Euclid's Algorithm for Greatest Common Divisor and Modular Division, Study notes of Algorithms and Programming

Partial preview of the text

Download Euclid's Algorithm for Greatest Common Divisor and Modular Division and more Study notes Algorithms and Programming in PDF only on Docsity!

Design and analysis of algorithmsLecture 38 & 39& 40^ Edyta Szyma ´nska^ [email protected]

CS3510 A, Fall 2005 – p. 1/

Euclid’s algorithm for greatestcommon divisor Given two integers^ a^ and^ b , compute the largest integer whichdivides both of them.

CS3510 A, Fall 2005 – p. 2/

Euclid’s algorithm for greatestcommon divisor Given two integers^ a^ and^ b , compute the largest integer whichdivides both of them. Example: GCD(1035,^ 759) =?^2 If we can factorize, then^ 1035 = 3·^5 ·^23 and 759 = 3 · 11 · 23 and thus GCD(1035,^ 759) = 3

·^ 23 = 69.

No polynomial procedure is known for factoring integers.Different method must be used.

CS3510 A, Fall 2005 – p. 2/

Euclid’s algorithm for greatestcommon divisor Idea: Proposition If a > b^ then gcd (a, b) =gcd (a

mod^ b, b)^ CS3510 A, Fall 2005 – p. 3/

Euclid’s algorithm for greatestcommon divisor Idea: Proposition If a > b^ then gcd (a, b) =gcd (a

mod^ b, b) function^ EUCLID(a, b) Input: two positive integers

a, b^ with^ a^ ≥^ b Output: gcd(a, b) if^ b^ = 0^ then return^ a return^ EUCLID(b, a^ mod^ b) Running time?

CS3510 A, Fall 2005 – p. 3/

Euclid’s algorithm for greatestcommon divisor Idea: Proposition If a > b^ then gcd (a, b) =gcd (a

mod^ b, b) function^ EUCLID(a, b) Input: two positive integers

a, b^ with^ a^ ≥^ b Output: gcd(a, b) if^ b^ = 0^ then return^ a return^ EUCLID(b, a^ mod^ b) Running time? Claim^ If^ a^ ≥^ b^ then^ a^ mod

a b ≤. 2 Number of recursive calls:

2 ⌈logb⌉.^2 Since^ n^ =^ ⌈loga⌉^ +^ ⌈logb^2

⌉, we have^ O(n)^ rounds and the (^2 3) total time is O(n · n=^ n)^ (^2) (nfor division.)^ CS3510 A, Fall 2005 – p. 3/

An extension to Euclid’s algorithm How can we check that^ d^ is the greatest common divisor of a and b?

CS3510 A, Fall 2005 – p. 4/

An extension to Euclid’s algorithm How can we check that^ d^ is the greatest common divisor of a and b? Claim If d|a and^ d|b^ and^ d^ =^ ax^ +^ by^ for some integers x, y, then d = gcd(a, b). Claim For any inputs^ a, b,^ the Extended Euclid algorithmreturns integers^ x, y, d^ such that^ gcd(a, b

) =^ d^ =^ ax^ +^ by.^ CS3510 A, Fall 2005 – p. 4/

An extension to Euclid’s algorithm How can we check that^ d^ is the greatest common divisor of a and b? Claim If d|a and^ d|b^ and^ d^ =^ ax^ +^ by^ for some integers x, y, then d = gcd(a, b). Claim For any inputs^ a, b,^ the Extended Euclid algorithmreturns integers^ x, y, d^ such that^ gcd(a, b

) =^ d^ =^ ax^ +^ by. function^ EXTENDED-EUCLID(a, b

Input: two positive integers

a, b^ with^ a^ ≥^ b Output: integers^ x, y, d^ such that

d^ =^ gcd(a, b) =^ ax^ +^ by if^ b^ = 0^ then return^ (1,^0 , a) ′′(x, y, d) =EXTENDED-EUCLID

(b, a^ mod^ b) a′′ ′ return (y, x− ⌊⌋y, d)^ b^

CS3510 A, Fall 2005 – p. 4/

Modular division^1 In regular arithmetic: ∀a 6 = 0, a^ has an inverse^.^ a^ In modular arithmetic: x is a multiplicative inverse of

a^ if ax^ ≡^ 1(^ mod^ m). −^1 We denote^ x^ :=^ a( at most one such

x^ modulo^ m^ exists (why?)).

CS3510 A, Fall 2005 – p. 5/

Modular division^1 In regular arithmetic: ∀a 6 = 0, a^ has an inverse^.^ a^ In modular arithmetic: x is a multiplicative inverse of

a^ if ax^ ≡^ 1(^ mod^ m). −^1 We denote^ x^ :=^ a( at most one such

x^ modulo^ m^ exists (why?)). Proposition^ For any^ a < m, a

has a multiplicative inverse modulo^ m^ if and only if it is relatively prime to

m.^ When this inverse exists then it can be found in time

(^3) O(logm). Use Extended-Euclid’s algorithm to find the inverse of a if

a and^ m^ are relatively prime.

CS3510 A, Fall 2005 – p. 5/

  • Euclid’s algorithm for greatestcommon divisor Given two integers a and b , compute the largest integer whichdivides both of them. Example: GCD(1035, 759) =?^2 If we can factorize, then 1035 = 3·^5 ·^23 and 759 = 3 · 11 · 23 and thus GCD(1035, 759) =
    • · 23 = 69. CS3510 A, Fall 2005 – p. 2/
  • Modular division^1 In regular arithmetic: ∀a 6 = 0, a has an inverse a CS3510 A, Fall 2005 – p. 5/
  • Modular division^1 In regular arithmetic: ∀a 6 = 0, a has an inverse a CS3510 A, Fall 2005 – p. 5/
  • Primality testing Theorem (Fermat’s Little Theorem) If m is prime thenfor every 1 ≤ a < m, we have m− 1 a≡ 1( mod m). CS3510 A, Fall 2005 – p. 6/