Prime Numbers
Prime Numbers Prime Numbers
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).
- Page 186 and 187: 174 Chapter 4 PRIMALITY PROVING Rem
- Page 188 and 189: 176 Chapter 4 PRIMALITY PROVING sma
- Page 190 and 191: 178 Chapter 4 PRIMALITY PROVING Sin
- Page 192 and 193: 180 Chapter 4 PRIMALITY PROVING Let
- Page 194 and 195: 182 Chapter 4 PRIMALITY PROVING Rec
- Page 196 and 197: 184 Chapter 4 PRIMALITY PROVING (mo
- Page 198 and 199: 186 Chapter 4 PRIMALITY PROVING pol
- Page 200 and 201: 188 Chapter 4 PRIMALITY PROVING if
- Page 202 and 203: 190 Chapter 4 PRIMALITY PROVING 4.3
- Page 204 and 205: 192 Chapter 4 PRIMALITY PROVING j =
- Page 206 and 207: 194 Chapter 4 PRIMALITY PROVING The
- Page 208 and 209: 196 Chapter 4 PRIMALITY PROVING The
- Page 210 and 211: 198 Chapter 4 PRIMALITY PROVING Rem
- Page 212 and 213: 200 Chapter 4 PRIMALITY PROVING pos
- Page 214 and 215: 202 Chapter 4 PRIMALITY PROVING Alg
- Page 216 and 217: 204 Chapter 4 PRIMALITY PROVING fac
- Page 218 and 219: 206 Chapter 4 PRIMALITY PROVING 196
- Page 220 and 221: 208 Chapter 4 PRIMALITY PROVING The
- Page 222 and 223: 210 Chapter 4 PRIMALITY PROVING Say
- Page 224 and 225: 212 Chapter 4 PRIMALITY PROVING But
- Page 226 and 227: 214 Chapter 4 PRIMALITY PROVING for
- Page 228 and 229: 216 Chapter 4 PRIMALITY PROVING so
- Page 230 and 231: 218 Chapter 4 PRIMALITY PROVING (2)
- Page 232 and 233: 220 Chapter 4 PRIMALITY PROVING hav
- Page 234 and 235: 222 Chapter 4 PRIMALITY PROVING sho
- Page 238 and 239: 5.1 Squares 227 5.1.2 Lehman method
- Page 240 and 241: 5.2 Monte Carlo methods 229 That is
- Page 242 and 243: 5.2 Monte Carlo methods 231 It is c
- Page 244 and 245: 5.2 Monte Carlo methods 233 computi
- Page 246 and 247: 5.3 Baby-steps, giant-steps 235 cal
- Page 248 and 249: 5.4 Pollard p − 1 method 237 can
- Page 250 and 251: 5.6 Binary quadratic forms 239 f(jB
- Page 252 and 253: 5.6 Binary quadratic forms 241 so o
- Page 254 and 255: 5.6 Binary quadratic forms 243 equi
- Page 256 and 257: 5.6 Binary quadratic forms 245 is a
- Page 258 and 259: 5.6 Binary quadratic forms 247 In t
- Page 260 and 261: 5.6 Binary quadratic forms 249 of D
- Page 262 and 263: 5.7 Exercises 251 is completely rig
- Page 264 and 265: 5.7 Exercises 253 of each of these
- Page 266 and 267: 5.8 Research problems 255 5.17. Sho
- Page 268 and 269: 5.8 Research problems 257 modulo th
- Page 270 and 271: 5.8 Research problems 259 In judgin
- Page 272 and 273: 262 Chapter 6 SUBEXPONENTIAL FACTOR
- Page 274 and 275: 264 Chapter 6 SUBEXPONENTIAL FACTOR
- Page 276 and 277: 266 Chapter 6 SUBEXPONENTIAL FACTOR
- Page 278 and 279: 268 Chapter 6 SUBEXPONENTIAL FACTOR
- Page 280 and 281: 270 Chapter 6 SUBEXPONENTIAL FACTOR
- Page 282 and 283: 272 Chapter 6 SUBEXPONENTIAL FACTOR
- Page 284 and 285: 274 Chapter 6 SUBEXPONENTIAL FACTOR
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).