The n1 and n+1 primality tests

Determining if a number n is a prime number or not is an important problem in computational number theory. Two simple ways of proving primality rely on the prime factorizations of n1 and n+1. In general finding these factorizations is probably a harder problem than testing the primality of n, so the methods are only applicable in special cases, but are they are interesting nonetheless.

At a high level, the n1 method works by showing that a subgroup of Zn is so large that n must be prime. Specifically, a subgroup of order n1 is demonstrated, which implies that Zn is as large as possible, namely the full set of nonzero residues {1,2,,n1}; if even one element was missing then there would be less than n1 elements in Zn. But Zn={1,2,,n1} means that every positive integer strictly less than n is coprime to n, and so n is prime.

To show that Zn contains a subgroup of size n1, we find an element aZn whose order is n1 (such an element is called a primitive root). To do this, we show that

(1)an11(modn)

and

(2)a(n1)/p1(modn)

for all primes p which divide n1. This is enough to show that the order of a is n1; if the true order was r<n1 then using Bézout’s identity one would be able to derive agcd(r,n1)1(modn) and this would contradict (2) for some p.

When n is prime, there isn’t any known efficient algorithm which is guaranteed to find a primitive root a satisfying (1) and (2), but in practice this isn’t a concern. In fact, there are φ(n1)=Θ(n/lnlnn) primitive roots (where φ denotes Euler’s totient function), so if one simply tests if random a satisfies (1) and (2) then one should quickly find one which works, and thereby prove that n is prime. As mentioned, the real problem with applying this method in practice is finding the primes p which divide n1.

Incidentally, when n isn’t prime, this is usually easy to show since condition (1) will often fail to hold, and all primes satisfy (1) for all aZn. However, some composite numbers still satisfy (1) for all aZn. In such a case a more stringent form of (1) can be used to prove compositeness, and this method can be employed in practice since it only requires the “evenness factorization” n1=2rm with m odd, which is simple to compute.

The n+1 method is similar to the n1 method, except it works in the group (Zn[d]), where d satisfies (dn)=1, i.e., d is not a quadratic residue mod n. We assume that n is odd (otherwise the problem is trivial), so that in practice d can be found by computing the Jacobi symbol (dn) for multiple values of d until one works. Note that when n is prime we have Zn[d]Fn2, the finite field of size n2, so there still exist primitive roots a which generate (Zn[d]). We’ll denote the conjugate of a by a¯, so if a:=b+cd with b, cZn then a¯=bcd.

As you might expect in relation to the above, the n+1 method finds a subgroup of (Zn[d]) of size n+1 by finding an a(Zn[d]) which has order n+1. Actually, we need something a bit stronger than this; we need to show

(3)an+11(modn)

and

(4)gcd((a(n+1)/p1)(a¯(n+1)/p1),n)=1

for all primes p which divide n+1. Condition (4) not only implies that a(n+1)/p1(modn), but also that a(n+1)/p1(modq) where q is any prime divisor of n. To see that this, we show the contrapositive. Suppose that q is a prime divisor of n and that qa(n+1)/p1.1 It follows that q also divides (a(n+1)/p1)(a¯(n+1)/p1)Z, and thus divides the gcd in (4), so (4) fails to hold.

Since condition (3) implies that an+11(modq) and condition (4) implies that a(n+1)/p1(modq), just like before one knows that a(modq) has order n+1. In particular, there must be at least n+1 elements in (Zq[d]).

Toward a contradiction, suppose that (3) and (4) hold and that n is not prime. Let q be the smallest prime divisor of n, so that q2n. As we just saw, a(modq) has order n+1, so we get the lower bound

n+1|(Zq[d])|.

However, Zq[d] has q2 elements, and so

|(Zq[d])|q21n1,

which contradicts the lower bound. Thus n must in fact be prime.

When n is prime, an a which satisfies (3) and (4) can be found by taking a primitive root of Fn2 and raising it to the power n1, since in this case (an1)n+11(modn) and n will not divide (an1)(n+1)/p1 or its conjugate. As mentioned, there isn’t a guaranteed procedure to find a primitive root, but since there are φ(n21)=Θ(n2/lnlnn) of them, in practice it shouldn’t be too hard to find one; the sticking point is finding the factorization of n+1.

Incidentally, the n+1 test is often presented using Lucas sequences rather than Fn2. But that’s a topic for another post.

  1. This notation means that there is some algebraic integer k such that qk=a(n+1)/p1. However, it is actually only necessary to consider kZ[d], since we can take aZ[d] and q is odd. ↩