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

Lecture notes from cs3510 a, fall 2005, covering euclid's algorithm for finding the greatest common divisor (gcd) of two integers and the concept of modular division. The notes include the idea behind euclid's algorithm, its implementation, and the running time analysis. Additionally, an extension to euclid's algorithm for checking if a number is the gcd is discussed, as well as the concept of modular division and its inverse.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-ul1
koofers-user-ul1 🇺🇸

9 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Design and analysis of algorithms
Lecture 38 & 39& 40
Edyta Szyma´
nska
CS3510 A, Fall 2005 p. 1/15
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

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/