Prime Numbers
Prime Numbers Prime Numbers
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,
- Page 412 and 413: 8.2 Random-number generation 403 is
- Page 414 and 415: 8.3 Quasi-Monte Carlo (qMC) methods
- Page 416 and 417: 8.3 Quasi-Monte Carlo (qMC) methods
- Page 418 and 419: 8.3 Quasi-Monte Carlo (qMC) methods
- Page 420 and 421: 8.3 Quasi-Monte Carlo (qMC) methods
- Page 422 and 423: 8.3 Quasi-Monte Carlo (qMC) methods
- Page 424 and 425: 8.4 Diophantine analysis 415 [Tezuk
- Page 426 and 427: 8.4 Diophantine analysis 417 9262 3
- Page 428 and 429: 8.5 Quantum computation 419 We spea
- Page 430 and 431: 8.5 Quantum computation 421 three H
- Page 432 and 433: 8.5 Quantum computation 423 for a n
- Page 434 and 435: 8.6 Curious, anecdotal, and interdi
- Page 436 and 437: 8.6 Curious, anecdotal, and interdi
- Page 438 and 439: 8.6 Curious, anecdotal, and interdi
- Page 440 and 441: 8.7 Exercises 431 universal Golden
- Page 442 and 443: 8.7 Exercises 433 standards insist
- Page 444 and 445: 8.7 Exercises 435 of positive compo
- Page 446 and 447: 8.8 Research problems 437 element o
- Page 448 and 449: 8.8 Research problems 439 the Leveq
- Page 450 and 451: 8.8 Research problems 441 for every
- Page 452 and 453: Chapter 9 FAST ALGORITHMS FOR LARGE
- Page 454 and 455: 9.1 Tour of “grammar-school” me
- Page 456 and 457: 9.2 Enhancements to modular arithme
- Page 458 and 459: 9.2 Enhancements to modular arithme
- Page 460 and 461: 9.2 Enhancements to modular arithme
- Page 464 and 465: 9.2 Enhancements to modular arithme
- Page 466 and 467: 9.3 Exponentiation 457 Algorithm 9.
- Page 468 and 469: 9.3 Exponentiation 459 But there is
- Page 470 and 471: 9.3 Exponentiation 461 the benefit
- Page 472 and 473: 9.4 Enhancements for gcd and invers
- Page 474 and 475: 9.4 Enhancements for gcd and invers
- Page 476 and 477: 9.4 Enhancements for gcd and invers
- Page 478 and 479: 9.4 Enhancements for gcd and invers
- Page 480 and 481: 9.4 Enhancements for gcd and invers
- Page 482 and 483: 9.5 Large-integer multiplication 47
- Page 484 and 485: 9.5 Large-integer multiplication 47
- Page 486 and 487: 9.5 Large-integer multiplication 47
- Page 488 and 489: 9.5 Large-integer multiplication 47
- Page 490 and 491: 9.5 Large-integer multiplication 48
- Page 492 and 493: 9.5 Large-integer multiplication 48
- Page 494 and 495: 9.5 Large-integer multiplication 48
- Page 496 and 497: 9.5 Large-integer multiplication 48
- Page 498 and 499: 9.5 Large-integer multiplication 48
- Page 500 and 501: 9.5 Large-integer multiplication 49
- Page 502 and 503: 9.5 Large-integer multiplication 49
- Page 504 and 505: 9.5 Large-integer multiplication 49
- Page 506 and 507: 9.5 Large-integer multiplication 49
- Page 508 and 509: 9.5 Large-integer multiplication 49
- Page 510 and 511: 9.5 Large-integer multiplication 50
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,