We now consider division with remainder we want to

Info icon This preview shows pages 17–19. Sign up to view the full content.

View Full Document Right Arrow Icon
We now consider division with remainder. We want to compute q and r such that a = bq + r and 0 r < b . Let us assume that a b ; otherwise, we can just set q = 0 and r = a . Also, let us assume that b - 1 6 = 0. The quotient q will have at most m = k - + 1 base- B digits. Write q = ( q m - 1 · · · q 0 ) B . We compute the digits of q and the value r with the following division with remainder algorithm. 12
Image of page 17

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

View Full Document Right Arrow Icon
r a for i m - 1 down to 0 do q i ← b r/B i b c r r - B i · q i b To verify that this procedure is correct, one easily verifies by induction that in each loop iteration, r < B i +1 b . It is perhaps not immediately clear how to efficiently implement the step q i ← b r/B i b c . As in the paper-and-pencil one has to make a reasonable “guess” at q i , and then correct the guess if it turns out to be wrong. More generally, consider the following situation. Let x and y be positive integers with x/y < B , and let d = b x/y c . Suppose B = 2 t and that y is has n + t bits in its binary representation, where n 0. Then we can write y = ˆ y 2 n + y 0 , where 2 t - 1 ˆ y < 2 t and 0 y 0 < 2 n . We can also write x = ˆ x 2 n + x 0 , where 0 ˆ x < 2 2 t and 0 x 0 < 2 n . Then we can approximate d by ˆ d = b ˆ x/ ˆ y c . Theorem 3.1 With notation as in the previous paragraph, we have d ˆ d d + 2 . Proof. To prove the first inequality, it suffices to show that x - ˆ dy < y . Using the fact that ˆ x = ˆ d ˆ y + ˆ z , where 0 ˆ z < ˆ y , we have x - ˆ dy x - ˆ d ˆ y 2 n x - x - y - 1))2 n = x - ˆ x 2 n + (ˆ y - 1)2 n < 2 n + (ˆ y - 1)2 n = ˆ y 2 n y. That proves the first inequality. To prove the second inequality, it suffices to show that x - ˆ dy ≥ - 2 y . We have x - ˆ dy x - ˆ d y 2 n + 2 n ) = x - ˆ d ˆ y 2 n - ˆ d 2 n x - ˆ x 2 n - ˆ d 2 n ≥ - ˆ d 2 n . So it suffices to show that ˆ d 2 n /y 2. Using the fact that ˆ y 2 t - 1 , we have ˆ d 2 n y ˆ x 2 n ˆ yy x ˆ yy 2 t ˆ y 2 t 2 t - 1 = 2 . That proves the second inequality. 2 Now, going back to our division with remainder algorithm, consider executing one iteration of the main loop. If i = 0 and b < B , then we can compute b r/b c in a single step, using the “built-in” division instruction; otherwise, we apply the above theorem with y := B i b B and x := r < Bx . We can extract the high-order t bits from y and the corresponding high-order bits of x , and with one division of a number with less than 2 t -bits by a t -bit number, we get an approximation ˆ q i to q i . All of this can be carried out in time O (1). With the above theorem, q i ˆ q i q i + 2. We perform the subtraction step r r - B i · ˆ q i b , which takes time O ( ). At this point, we can easily detect if our approximation was too large. Correcting the values of ˆ q i and r then can be done in time O ( ). Thus, each loop iteration takes time O ( ), and hence the total running-time of this division with remainder algorithm is O ( m‘ ).
Image of page 18
Image of page 19
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