Skip to main content

Proving Primality

note

This is Lab 13 from Clint by Hugh Montgomery, edited by Codex and Elad Zelingher. All rights reserved to Hugh Montgomery.

We have seen that a composite number can be proved to be composite very quickly by means of the strong pseudoprime test. Finding the complete factorization of a composite number takes longer, but for large nn we have methods that are much faster than trial division. To complete the picture we need a fast method for proving the primality of a large prime number pp. In this direction, we show that proving the primality of pp is no harder than factoring p1p-1. Suppose that gp11(modp)g^{p-1}\equiv1\pmod p, and that g(p1)/q≢1(modp)g^{(p-1)/q}\not\equiv1\pmod p for every prime factor qq of p1p-1. Then gg has order p1p-1 modulo pp, and hence pp must be prime. In general, if pp is prime then such a gg is not hard to find; thus we have a means of proving that pp is prime provided that we can factor p1p-1.

Problem 1

Apply PrimRoot to 8675309. The machine quickly responds with a primitive root, because 8675308 is easily factored. Thus the primality of 8675309 has been rigorously established. (David Farmer proposes that this is the largest prime number ever mentioned in a popular song.)

The simple idea used above can be strengthened in several ways. First, suppose that qq is a prime factor of p1p-1, say qk(p1)q^k\mathbin\Vert(p-1). Let pp' denote an arbitrary prime factor of pp. Presumably the only such pp' is p=pp'=p, but this remains to be proved. Suppose that we can find a number aa such that ap11(modp)a^{p-1}\equiv1\pmod p but such that (a(p1)/q1,p)=1(a^{(p-1)/q}-1,p)=1. Then ap11(modp)a^{p-1}\equiv1\pmod{p'} but a(p1)/q≢1(modp)a^{(p-1)/q}\not\equiv1\pmod{p'}. (Note that we can deduce this without knowing the value of pp'.) Hence qkq^k divides the order of aa modulo pp', and consequently qk(p1)q^k\mid(p'-1). That is, every prime factor pp' of pp is 1(modqk)\equiv1\pmod{q^k}. Suppose we repeat this for several different prime factors qq of p1p-1. (The value of aa that works is allowed to depend on qq.) Let ss denote the product of the primepowers qkq^k for which this calculation has succeeded. Then we can assert that every prime factor pp' of pp is 1(mods)\equiv1\pmod s. Since the product of two or more such primes must be >s2>s^2, we see that if s>ps>\sqrt p then pp must be prime. Hence we can establish the primality of pp, based only on an incomplete factorization of p1p-1, provided that we can factor ss, s(p1)s\mid(p-1), and s>ps>\sqrt p. (This analysis is related to Problems 38, 39 at the end of §2.8 of NZM.)

Problem 2

It is easy to confirm that 716+516=2166927592301137^{16}+5^{16}=2\cdot16692759230113. Use the program ProveP to demonstrate that this second factor is prime. That is, apply ProveP to 16692759230113, and note the results.

Problem 3

If, as the factorization of p1p-1 proceeds, a point is reached at which the factored portion ss of p1p-1 is so large that testing pp for divisors d1(mods)d\equiv1\pmod s, dpd\leq\sqrt p will take less time than the time already spent trying to factor p1p-1, then the program ProveP automatically switches to the latter approach. To witness an instance of this, apply ProveP to the number 51017+21=5000000000000000215\cdot10^{17}+21=500000000000000021.

The method of proving primality being employed here can be made still more efficient. Suppose that as prime factors qq of p1p-1 are being found, we reach a point at which p1/3<sp1/2p^{1/3}<s\leq p^{1/2}. Then either pp is prime or pp is the product of two primes, p=ppp=p'p'', say. Write p=as+1p'=a's+1 and p=as+1p''=a''s+1, so that p=aas2+(a+a)s+1p=a'a''s^2+(a'+a'')s+1. With a little care with inequalities, it can be shown that 0<aa<s0<a'a''<s and that 0<a+a<s0<a'+a''<s. Thus the representation of pp that we have given here in terms of powers of ss coincides with the expansion of pp in base ss. That is, by the division algorithm we may write p=c2s2+c1s+1p=c_2s^2+c_1s+1 with 0ci<s0\leq c_i<s, and c1=a+ac_1=a'+a'', c2=aac_2=a'a''. To determine whether such aa' and aa'' exist, we have only to test whether c124c2c_1^2-4c_2 is a perfect square. This embellishment is due to H. C. Williams.

Problem 4

Apply the program ProveP to demonstrate that the number 12345678975311234567897531 is prime. Also that 975312468097531975312468097531 is prime. If the program GetNextP is given an argument xx for which 109<x<101810^9<x<10^{18}, then the number pp returned is the least integer >x>x that is a strong pseudoprime to bases 2, 3, 5, 7 and 11. It is incredibly likely that pp is prime, but to obtain a rigorous proof one should use the program ProveP. In this manner, find the least prime greater than 12345678987654321.

Problem 5

In most cases the method used by ProveP is reasonably quick. However, it can happen that p1=2qp-1=2q where qq is prime. In such a case, attention is focused on qq. After a brief attempt to factor it by trial division fails, one should apply a strong pseudoprime test. If qq passes the test, then a rigorous proof that qq is prime may be obtained by applying the program ProveP to qq. In attempting to factor q1q-1 one may encounter the same problem as with p1p-1. Nevertheless, by systematically employing the programs ProveP, SPsP, Factor and Rho, the needed factorizations can be rigorously established. For example, suppose that we apply the program ProveP to show that the number p=987292984329259p=987292984329259 is prime. The machine quickly finds that p1p-1 is divisible by 2 and by 3, but then there is a pause. Touch any key to interrupt the program, and you find that it is trying to factor 164548830721543. Type q to quit, and then apply the program SPsP to this factor. In this way we discover that we are dealing with a composite number, so we apply the program Rho, which discloses that the factor may be written as 5378033305964715378033\cdot30596471. We apply the program Factor to the first of these numbers, to confirm that it is prime. Then we again apply ProveP to the original number pp. Again the machine finds 2 and 3, but when it pauses, we interrupt it, type s to indicate that we wish to supply a prime factor, and enter 5378033. This time the program reaches a successful resolution without further intervention, and it is proved that pp is indeed prime. Show that 101811=99999999999999998910^{18}-11=999999999999999989 is prime. What intermediate numbers need to be factored? Give an account of the programs used, and the findings. (By applying GetNextP to this number you may confirm that this is the largest prime not exceeding 101810^{18}. Similarly, show that (10187)/3=333333333333333331(10^{18}-7)/3=333333333333333331 is prime. Indicate what numbers are encountered, and how they are dealt with.

Problem 6

Show that 1017+1910^{17}+19 and 1017+2110^{17}+21 are both prime.

Problem 7

What is the first composite number in the sequence 31, 331, 3331, 33331, \ldots? Apply ProveP until the first composite element is encountered. Here the kk-th term is uk=(10k7)/3u_k=(10^k-7)/3. Show that for every kk, the least prime factor dividing uku_k is 17\geq17. Show that 17uk17\mid u_k precisely when kk lies in a certain residue class (mod 16). (Suggestion: Use the program PowerTab to display 10k(modm)10^k\pmod m for appropriate mm.)

Problem 8

For the programmer. When attempting to prove that pp is prime, we factor p1p-1. After removing the factor 2, this leaves p1=(p1)/2p_1=(p-1)/2 to be factored—but this may be prime. In such a case we would confirm that p1p_1 is prime by applying ProveP. However, it may happen that p1=2p2+1p_1=2p_2+1 with p2p_2 prime. Perhaps also p2=2p3+1p_2=2p_3+1 with p3p_3 prime. It is in such a case that our procedure for establishing primality will require the most work. How long can a chain of primes be, with pi=2pi+1+1p_i=2p_{i+1}+1? Construct a program to explore this. Apply the SPSP test to odd integers until a probable prime pp is found. Then apply SPSP to 2p+12p+1, and so on, until a chain of kk probable primes has been constructed. If this chain is of record-breaking length, print out pp and kk, so that the program ProveP can be applied to the members of the chain. The first chain you will find is 33, 77; the next one is 55, 1111, 2323, 4747. Show that if pp begins a chain of length at least 4, with p>5p>5, then necessarily p29(mod30)p\equiv29\pmod{30}. For more on such chains, see D. H. Lehmer, On certain chains of primes, Proc. London Math. Soc. (3) 14a (1965), 183–186.

By the method of primality proof employed here, we see that proving that pp is prime is no harder than factoring p1p-1. Other methods of proving primality have been proposed, and some of these are significantly more efficient than our best factoring algorithms. Thus a prime of 1000 digits can be proved to be prime, but the record for factoring hard composite numbers stands below 200 digits. One of the methods currently in wide use is that of Adleman, Pomerance and Rumely (the APR method); it depends on Gauss sums. A method depending on elliptic curves, devised by Atkin and Morain, has achieved some striking successes lately. For more information concerning proofs of primality, consult the following papers.

L. M. Adleman, C. Pomerance, and R. S. Rumely, On distinguishing prime numbers from composite numbers, Ann. of Math. (2) 117 (1983), 173–206.

A. O. L. Atkin and F. Morain, Elliptic curves and primality proving, Math. Comp. 61 (1993), 29–68.

J. D. Dixon, Factorization and primality tests, Amer. Math. Monthly. 91 (1984), 333–352.