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

Chapter 5 EXPONENTIAL FACTORING ALGORITHMS For almost all of the multicentury history of factoring, the only algorithms available were exponential, namely, the running time was, in the worst case, a fixed positive power of the number being factored. But in the early 1970s, subexponential factoring algorithms began to come “on line.” These methods, discussed in the next chapter, have their running time to factor n bounded by an expression of the form n o(1) . One might wonder, then, why the current chapter exists in this book. We have several reasons for including it. (1) An exponential factoring algorithm is often the algorithm of choice for small inputs. In particular, in some subexponential methods, smallish auxiliary numbers are factored in a subroutine, and such a subroutine might invoke an exponential factoring method. (2) In some cases, an exponential algorithm is a direct ancestor of a subexponential algorithm. For example, the subexponential elliptic curve method grew out of the exponential p − 1 method. One might think of the exponential algorithms as possible raw material for future developments, much as various wild strains of agricultural cash crops are valued for their possible future contributions to the plant gene pool. (3) It is still the case that the fastest, rigorously analyzed, deterministic factoring algorithm is exponential. (4) Some factoring algorithms, both exponential and subexponential, are the basis for analogous algorithms for discrete logarithm computations. For some groups the only discrete logarithm algorithms we have are exponential. (5) Many of the exponential algorithms are pure delights. We hope then that the reader is convinced that this chapter is worth it! 5.1 Squares An old strategy to factor a number is to express it as the difference of two nonconsecutive squares. Let us now expand on this theme. 5.1.1 Fermat method If one can write n in the form a 2 − b 2 ,wherea, b are nonnegative integers, then one can immediately factor n as (a + b)(a − b). If a − b>1, then the

226 Chapter 5 EXPONENTIAL FACTORING ALGORITHMS factorization is nontrivial. Further, every factorization of every odd number n arises in this way. Indeed, if n is odd and n = uv, whereu, v are positive integers, then n = a2 − b2 with a = 1 1 2 (u + v) andb = 2 |u − v|. For odd numbers n that are the product of two nearby integers, it is easy to find a valid choice for a, b and so to factor n. For example, consider n = 8051. Thefirstsquareabovenis 8100 = 902 , and the difference to n is 49 = 72 .So 8051 = (90 + 7)(90 − 7) = 97 · 83. To formalize this as an algorithm, we take trial values of the number a from the sequence √ n , √ n +1,...and check whether a2−n is a square. If it is, say b2 ,thenwehaven = a2−b2 =(a+b)(a−b). For n odd and composite, this procedure must terminate with a nontrivial factorization before we reach a = ⌊(n +9)/6⌋. The worst case occurs when n =3p with p prime, in which case the only choice for a that gives a nontrivial factorization is (n+9)/6 (and the corresponding b is (n − 9)/6). Algorithm 5.1.1 (Fermat method). We are given an odd integer n > 1. This algorithm either produces a nontrivial divisor of n or proves n prime. 1. [Main loop] for ⌈ √ n⌉≤a ≤ (n +9)/6 { // Next, apply Algorithm 9.2.11. if b = √ a 2 − n is an integer return a − b; } return “n is prime”; It is evident that in the worst case, Algorithm 5.1.1 is much more tedious than trial division. But the worst cases for Algorithm 5.1.1 are actually the easiest cases for trial division, and vice versa, so one might try to combine the two methods. There are various tricks that can be used to speed up the Fermat method. For example, via congruences it may be discerned that various residue classes for a make it impossible for a 2 − n to be a square. As an illustration, if n ≡ 1 (mod 4), then a cannot be even, or if n ≡ 2 (mod 3), then a must be a multiple of 3. In addition, a multiplier might be used. As we have seen, if n is the product of two nearby integers, then Algorithm 5.1.1 finds this factorization quickly. Even if n does not have this product property, it may be possible for kn to be a product of two nearby integers, and gcd(kn, n) may be taken to obtain the factorization of n. For example, take n = 2581. Algorithm 5.1.1 has us start with a = 51 and does not terminate until the ninth choice, a = 59, where we find that 59 2 − 2581 = 900 = 30 2 and 2581 = 89 · 29. (Noticing that n ≡ 1(mod4),n ≡ 1 (mod 3), we know that a is odd and not a multiple of 3, so 59 would be the third choice if we used this information.) But if we try Algorithm 5.1.1 on 3n = 7743, we terminate on the first choice for a, namely a = 88, giving b =1.Thus3n =89· 87, and note that 89 = gcd(89,n), 29 = gcd(87,n).

226 Chapter 5 EXPONENTIAL FACTORING ALGORITHMS<br />

factorization is nontrivial. Further, every factorization of every odd number<br />

n arises in this way. Indeed, if n is odd and n = uv, whereu, v are positive<br />

integers, then n = a2 − b2 with a = 1<br />

1<br />

2 (u + v) andb = 2 |u − v|.<br />

For odd numbers n that are the product of two nearby integers, it is easy to<br />

find a valid choice for a, b and so to factor n. For example, consider n = 8051.<br />

Thefirstsquareabovenis 8100 = 902 , and the difference to n is 49 = 72 .So<br />

8051 = (90 + 7)(90 − 7) = 97 · 83.<br />

To formalize this as an algorithm, we take trial values of the number a<br />

from the sequence √ n , √ n +1,...and check whether a2−n is a square. If<br />

it is, say b2 ,thenwehaven = a2−b2 =(a+b)(a−b). For n odd and composite,<br />

this procedure must terminate with a nontrivial factorization before we reach<br />

a = ⌊(n +9)/6⌋. The worst case occurs when n =3p with p prime, in which<br />

case the only choice for a that gives a nontrivial factorization is (n+9)/6 (and<br />

the corresponding b is (n − 9)/6).<br />

Algorithm 5.1.1 (Fermat method). We are given an odd integer n > 1.<br />

This algorithm either produces a nontrivial divisor of n or proves n prime.<br />

1. [Main loop]<br />

for ⌈ √ n⌉≤a ≤ (n +9)/6 {<br />

// Next, apply Algorithm 9.2.11.<br />

if b = √ a 2 − n is an integer return a − b;<br />

}<br />

return “n is prime”;<br />

It is evident that in the worst case, Algorithm 5.1.1 is much more tedious than<br />

trial division. But the worst cases for Algorithm 5.1.1 are actually the easiest<br />

cases for trial division, and vice versa, so one might try to combine the two<br />

methods.<br />

There are various tricks that can be used to speed up the Fermat method.<br />

For example, via congruences it may be discerned that various residue classes<br />

for a make it impossible for a 2 − n to be a square. As an illustration, if n ≡ 1<br />

(mod 4), then a cannot be even, or if n ≡ 2 (mod 3), then a must be a multiple<br />

of 3.<br />

In addition, a multiplier might be used. As we have seen, if n is the product<br />

of two nearby integers, then Algorithm 5.1.1 finds this factorization quickly.<br />

Even if n does not have this product property, it may be possible for kn to<br />

be a product of two nearby integers, and gcd(kn, n) may be taken to obtain<br />

the factorization of n. For example, take n = 2581. Algorithm 5.1.1 has us<br />

start with a = 51 and does not terminate until the ninth choice, a = 59,<br />

where we find that 59 2 − 2581 = 900 = 30 2 and 2581 = 89 · 29. (Noticing that<br />

n ≡ 1(mod4),n ≡ 1 (mod 3), we know that a is odd and not a multiple of<br />

3, so 59 would be the third choice if we used this information.) But if we try<br />

Algorithm 5.1.1 on 3n = 7743, we terminate on the first choice for a, namely<br />

a = 88, giving b =1.Thus3n =89· 87, and note that 89 = gcd(89,n),<br />

29 = gcd(87,n).

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

Saved successfully!

Ooh no, something went wrong!