{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Thus each loop iteration takes time o ‘ and hence

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

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

View Full Document

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.

Unformatted text preview: Thus, each loop iteration takes time O ( ‘ ), and hence the total running-time of this division with remainder algorithm is O ( m‘ ). 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 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 > , we can compute q and r such that a = bq + r and ≤ 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 > . Define the numbers r ,r 1 ,...,r ‘ +1 , and q 1 ,...,q ‘ , where ‘ ≥ 1 , as follows: r = a, r 1 = b, r = 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 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 ,r 1 ) = gcd( r ‘ , 0) = r ‘ ....
View Full Document

{[ snackBarMessage ]}

### Page18 / 74

Thus each loop iteration takes time O ‘ and hence the...

This preview shows document pages 18 - 21. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online