Prime Numbers

Prime Numbers Prime Numbers

thales.doa.fmph.uniba.sk
from thales.doa.fmph.uniba.sk More from this publisher
10.12.2012 Views

9.2 Enhancements to modular arithmetic 453 where b = B(R) − 1. In particular, such splitting can render the relevant multiplications somewhat simpler. In fact, one sees that R x/2 b−1 /2 b+1 = ⌊x/N⌋−j (9.13) for j =0, 1, or 2. Thus using the left-hand side for d in Algorithm 9.2.10 involves at most two passes through the while loop. And there is an apparent savings in time, since the length of x can be about 2b, and the length of R about b. Thus the multiplication xR in Algorithm 9.2.10 is about 2b × b bits, while the multiplication inherent in (9.12) is only about b × b bits. Because a certain number of the bits of xR are destined to be shifted into oblivion (a shift completely obscures the relevant number of lower-order bits), one can intervene into the usual grammar-school multiply loop, effectively cutting the aforementioned rhombus into a smaller tableau of values. With considerations like this, it can be shown that for 0 ≤ x

454 Chapter 9 FAST ALGORITHMS FOR LARGE-INTEGER ARITHMETIC precision modes in such cases. Another new development is an interesting Karatsuba-like recursive divide, in [Burnikel and Ziegler 1998]. The method has the interesting property that the complexities of finding the div or just a mod result are not quite the same. Newton methods apply beyond the division problem. Just one example is the important computation of ⌊ √ N⌋. Onemayemploya(realdomain) Newton iteration for √ a in the form xn+1 = xn 2 to forge an algorithm for integer parts of square roots: a + , (9.14) 2xn Algorithm 9.2.11 (Integer part of square root). This algorithm returns ⌊ √ N⌋ for positive integer N. 1. [Initialize] x =2 ⌈B(N)/2⌉ ; 2. [Perform Newton iteration] y = ⌊(x + ⌊N/x⌋)/2⌋; if(y ≥ x) return x; x = y; goto [Perform Newton iteration]; We may use Algorithm 9.2.11 to test whether a given positive integer N is a square. After x = ⌊ √ N⌋ is computed, we do one more step and check whether x2 = N. This equation holds if and only if N is a square. Of course, there are other ways to rule out very quickly whether N is a perfect square, for example to test instances of ( N p ) for various small primes p, or the residue of N modulo 8. It can be argued that Algorithm 9.2.11 requires O(ln ln N) iterations to terminate. There are many interesting complexity issues with this and other Newton method applications. Specifically, it is often lucrative to change dynamically the working precision as the Newton iteration progresses, or to modify the very Newton loops (see Exercises 9.14 and 4.11). 9.2.3 Moduli of special form Considerable efficiency in the mod operation can be achieved when the modulus N is of special form. The Barrett method of the previous section is fast because it exploits mod 2 q arithmetic. In this section we shall see that if the modulus N is close to a power of 2, one can exploit the binary nature of modern computers and carry out the arithmetic very efficiently. In particular, forms N =2 q + c, where |c| is in some sense “small” (but c is allowed to be negative), admit efficient mod N operations. These enhancements are especially important in the studies of Mersenne primes p =2q− 1 and Fermat numbers Fn =22n+1,

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

precision modes in such cases. Another new development is an interesting<br />

Karatsuba-like recursive divide, in [Burnikel and Ziegler 1998]. The method<br />

has the interesting property that the complexities of finding the div or just a<br />

mod result are not quite the same.<br />

Newton methods apply beyond the division problem. Just one example<br />

is the important computation of ⌊ √ N⌋. Onemayemploya(realdomain)<br />

Newton iteration for √ a in the form<br />

xn+1 = xn<br />

2<br />

to forge an algorithm for integer parts of square roots:<br />

a<br />

+ , (9.14)<br />

2xn<br />

Algorithm 9.2.11 (Integer part of square root). This algorithm returns<br />

⌊ √ N⌋ for positive integer N.<br />

1. [Initialize]<br />

x =2 ⌈B(N)/2⌉ ;<br />

2. [Perform Newton iteration]<br />

y = ⌊(x + ⌊N/x⌋)/2⌋;<br />

if(y ≥ x) return x;<br />

x = y;<br />

goto [Perform Newton iteration];<br />

We may use Algorithm 9.2.11 to test whether a given positive integer N<br />

is a square. After x = ⌊ √ N⌋ is computed, we do one more step and check<br />

whether x2 = N. This equation holds if and only if N is a square. Of course,<br />

there are other ways to rule out very quickly whether N is a perfect square,<br />

for example to test instances of ( N<br />

p ) for various small primes p, or the residue<br />

of N modulo 8.<br />

It can be argued that Algorithm 9.2.11 requires O(ln ln N) iterations<br />

to terminate. There are many interesting complexity issues with this and<br />

other Newton method applications. Specifically, it is often lucrative to change<br />

dynamically the working precision as the Newton iteration progresses, or to<br />

modify the very Newton loops (see Exercises 9.14 and 4.11).<br />

9.2.3 Moduli of special form<br />

Considerable efficiency in the mod operation can be achieved when the<br />

modulus N is of special form. The Barrett method of the previous section<br />

is fast because it exploits mod 2 q arithmetic. In this section we shall see that<br />

if the modulus N is close to a power of 2, one can exploit the binary nature of<br />

modern computers and carry out the arithmetic very efficiently. In particular,<br />

forms<br />

N =2 q + c,<br />

where |c| is in some sense “small” (but c is allowed to be negative), admit<br />

efficient mod N operations. These enhancements are especially important in<br />

the studies of Mersenne primes p =2q− 1 and Fermat numbers Fn =22n+1,

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

Saved successfully!

Ooh no, something went wrong!