10.12.2012 Views

Prime Numbers

Prime Numbers

Prime Numbers

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

450 Chapter 9 FAST ALGORITHMS FOR LARGE-INTEGER ARITHMETIC<br />

a convenient value R = B k may be used, whence the z value in Algorithm<br />

9.2.5 can be obtained by looping k times and doing arithmetic (mod B) that<br />

is particularly convenient for the machine. Explicit word-oriented loops that<br />

achieve the optimal asymptotic complexity are laid out nicely in [Menezes et<br />

al. 1997].<br />

9.2.2 Newton methods<br />

We have seen in Section 9.1 that the div operation may be effected via<br />

additions, subtractions, and bit-shifts, although, as we have also seen, the<br />

algorithm can be bested by moving away from the binary paradigm into the<br />

domain of general base representations. Then we saw that the technique of<br />

Montgomery mod gives us an asymptotically efficient means for powering with<br />

respect to a fixed modulus. It is interesting, perhaps at first surprising, that<br />

general div and mod may be effected via multiplications alone; that is, even<br />

the small div operations attendant to optimized div methods are obviated, as<br />

are the special precomputations of the Montgomery method.<br />

One approach to such a general div and mod scheme is to realize that the<br />

classical Newton method for solving equations may be applied to the problem<br />

of reciprocation. Let us start with reciprocation in the domain of real numbers.<br />

If one is to solve f(x) = 0, one proceeds with an (adroit) initial guess for x,<br />

call this guess x0, and iterates<br />

xn+1 = xn − f(xn)/f ′ (xn), (9.9)<br />

for n =0, 1, 2 ..., whence—if the initial guess x0 is good enough—the sequence<br />

(xn) converges to the desired solution. So to reciprocate a real number a>0,<br />

one is trying to solve 1/x − a = 0, so that an appropriate iteration would be<br />

xn+1 =2xn − ax 2 n. (9.10)<br />

Assuming that this Newton iteration for reciprocals is successful (see Exercise<br />

9.13), we see that the real number 1/a can be obtained to arbitrary accuracy<br />

with multiplies alone. To calculate a general real division b/a, onesimply<br />

multiplies b by the reciprocal 1/a, so that general division in real numbers<br />

can be done in this way via multiplies alone.<br />

But can the Newton method be applied to the problem of integer div?<br />

Indeed it can, provided that we proceed with care in the definition of a<br />

generalized reciprocal for integer division. We first introduce a function B(N),<br />

defined for nonnegative integers N as the number of bits in the binary<br />

representation of N, except that B(0) = 0. Thus, B(1) = 1,B(2) = B(3) = 2,<br />

and so on. Next we establish a generalized reciprocal; instead of reciprocals<br />

1/a for real a, we consider a generalized reciprocal of integer N as the integer<br />

part of an appropriate large power of 2 divided by N.<br />

Definition 9.2.7. The generalized reciprocal R(N) is defined for positive<br />

integers N as ⌊4 B(N−1) /N ⌋.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!