# 3 into a simple algorithm as follows while b 6 0 do

This preview shows pages 20–23. Sign up to view the full content.

We can easily turn the scheme described in Theorem 3.3 into a simple algorithm as follows: while b 6 = 0 do Compute q, r such that a = bq + r , with 0 r < b ( a, b ) ( b, r ) output a By Theorem 3.3, this algorithm, known as Euclid’s algorithm , outputs the greatest common divisor of a and b . Theorem 3.4 Euclid’s algorithm runs in time O ( L ( a ) L ( b )) . Proof. The running time is O ( τ ), where τ = i =1 L ( r i ) L ( q i ). We have τ ≤ L ( b ) X i L ( q i ) ≤ L ( b ) X i (log 2 q i + 1) = L ( b )( + log 2 ( Y i q i )) . Note that a = r 0 r 1 q 1 r 2 q 2 q 1 ≥ · · · ≥ r q · · · q 1 q · · · q 1 . We also have log b/ log φ + 1. Combining this with the above, we have τ ≤ L ( b )(log b/ log φ + 1 + log 2 a ) = O ( L ( a ) L ( b )) , which proves the theorem. 2 Let d = gcd( a, b ). We know that there exist integers s and t such that as + bt = d . The extended Euclidean algorithm , which we now describe, allows us to compute s and t . 15

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Theorem 3.5 Let a , b , r 0 , r 1 , . . . , r +1 , and q 1 , . . . , q be as in Theorem 3.3. Define integers s 0 , s 1 , . . . , s +1 and t 0 , t 1 , . . . , t +1 as follows: s 0 := 1 , t 0 := 0 , s 1 := 0 , t 1 := 1 , and for 1 i , s i +1 := s i - 1 - s i q i , t i +1 := t i - 1 - t i q i . Then (i) for 0 i + 1 , we have s i a + t i b = r i ; in particular, s a + t b = gcd( a, b ) ; (ii) for 0 i , we have s i t i +1 - t i s i +1 = ( - 1) i ; (iii) for 0 i + 1 , we have gcd( s i , t i ) = 1 ; (iv) we have | s +1 | ≤ b and | t +1 | ≤ a ; (v) for 0 i , we have | t i | ≤ | t i +1 | , and for 1 i , we have | s i | ≤ | s i +1 | ; (vi) for 0 i + 1 , we have | s i | ≤ b and | t i | ≤ a . Proof. (i) and (ii) are easily proved by induction on i (exercise). (iii) follows directly from (ii). To prove (iv), note that s +1 a + t +1 b = r +1 = 0. We have t +1 6 = 0, since otherwise, both s +1 and t +1 would be zero, contradicting (ii). This implies that t +1 /s +1 = - a/b , and then (iv) follows from the fact (iii) that gcd( s +1 , t +1 ) = 1. For (v), one proves the statement for the t i by induction, but with the stronger hypothesis that t i t i +1 0 (i.e., the sign alternates) and | t i | ≤ | t i +1 | for 0 i (exercise). One argues similarly for the statement for the s i . (vi) follows immediately from (iv) and (v). 2 Example 3.2 We continue with Example 3.1. The numbers s i and t i are easily computed from the q i : i 0 1 2 3 4 r i 100 35 30 5 0 q i 2 1 6 s i 1 0 1 -1 7 t i 0 1 -2 3 -20 2 We can easily turn the scheme described in Theorem 3.5 into a simple algorithm, as follows: s 1 , t 0 s 0 0 , t 0 1 while b 6 = 0 do Compute q, r such that a = bq + r , with 0 r < b ( s, t, s 0 , t 0 ) ( s 0 , t 0 , s - s 0 q, t - t 0 q ) ( a, b ) ( b, r ) output a, s, t 16
This algorithm, known as the extended Euclidean algorithm , computes the greatest common divisor d of a and b , together with s and t such that as + bt = d . Theorem 3.6 The extended Euclidean algorithm runs in time O ( L ( a ) L ( b )) . Proof. It suffices to analyze the cost of computing the sequences { s i } and { t i } . Consider first the cost of computing all of the t i , which is O ( τ ), where τ = i =1 L ( t i ) L ( q i ). By Theorem 3.5 part (vi), and arguing as in the proof of Theorem 3.4, we have τ = L ( q 1 ) + X i =2 L ( t i ) L ( q i ) ≤ L ( q 1 ) + L ( a )( - 1 + log 2 ( Y i =2 q i )) = O ( L ( a ) L ( b )) , using the fact that Q i =2 q i b . An analogous argument shows that one can compute all of the

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern