{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

We now summarize the above observations for an

Info icon This preview shows pages 18–21. Sign up to view the full content.

View Full Document Right Arrow Icon
We now summarize the above observations. For an integer n , we define L ( n ) to be the number of bits in the binary representation of | n | ; more precisely, L ( n ) = b log 2 | n |c + 1 if n 6 = 0, 1 if n = 0. 13
Image of page 18

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

View Full Document Right Arrow Icon
Notice that for n > 0, log 2 n < L ( n ) log 2 n + 1. Theorem 3.2 Let a and b be arbitrary integers, represented using the data structures described above. (i) We can determine an arbitrary bit in the binary representation of | a | in time O (1) . (ii) We can compute a ± b in time O ( L ( a ) + L ( b )) . (iii) We can compute a · b in time O ( L ( a ) L ( b )) . (iv) If b > 0 , we can compute q and r such that a = bq + r and 0 r < b in time O ( L ( b ) L ( q )) . From now on, we shall not worry about the implementation details of long-integer arithmetic, and will just refer directly this theorem. Note the bound O ( L ( b ) L ( q )) in part (iv) of this theorem, which may be significantly less than the bound O ( L ( a ) L ( b )). This theorem does not refer to the base B in the underlying implementation. The choice of B affects the values of the implied big-‘O’ constants; while in theory, this is of no significance, it does have a significant impact in practice. We should point out that the algorithms discussed here for integer multiplication and division with remainder are by no means the best possible. If a and b are two integers whose length in bits is bounded by k , then the fastest known algorithm for computing ab runs in time O ( k log k log log k ). The fastest known algorithm to divide a by b also runs in time O ( k log k log log k ). We shall not discuss such fast algorithms any further here, even though in practice, they do indeed play a significant role, at least for numbers of more than a few hundred bits in length. 3.3 Greatest Common Divisors We consider the following problem: given two positive integers a and b , compute gcd( a, b ). We can do this using the well-known algorithm of Euclid, which is described in the following theorem. Theorem 3.3 Let a b > 0 . Define the numbers r 0 , r 1 , . . . , r +1 , and q 1 , . . . , q , where 1 , as follows: r 0 = a, r 1 = b, r 0 = r 1 q 1 + r 2 (0 < r 2 < r 1 ) , . . . r i - 1 = r i q i + r i +1 (0 < r i +1 < r i ) , . . . r - 2 = r - 1 q - 1 + r (0 < r < r - 1 ) , r - 1 = r q ( r +1 = 0) . Then r = gcd( a, b ) and log b/ log φ + 1 , where φ = (1 + 5) / 2 1 . 62 . 14
Image of page 19
Proof. For the first statement, one sees that for 1 i , the common divisors of r i - 1 and r i are the same as the common divisors of r i and r i +1 , and hence gcd( r i - 1 , r i ) = gcd( r i , r i +1 ). From this, it follows that gcd( a, b ) = gcd( r 0 , r 1 ) = gcd( r , 0) = r . To prove the second statement, we claim that for 0 i - 1, r - i φ i . The statement will then follow by setting i = - 1 and taking logarithms. If = 1, the claim is obviously true, so assume ‘ > 1. We have r 1 = φ 0 and r - 1 r + 1 2 φ 1 . For 2 i - 1, using induction and applying the fact the φ 2 = φ + 1, we have r - i r - ( i - 1) + r - ( i - 2) φ i - 1 + φ i - 2 = φ i - 2 (1 + φ ) = φ i , which proves the claim. 2 Example 3.1 Suppose a = 100 and b = 35. Then the numbers appearing in Theorem 3.3 are easily computed as follows: i 0 1 2 3 4 r i 100 35 30 5 0 q i 2 1 6 So we have gcd( a, b ) = r 3 = 5. 2 We can easily turn the scheme described in Theorem 3.3 into a simple algorithm as follows:
Image of page 20

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

View Full Document Right Arrow Icon
Image of page 21
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern