Determining if a number 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 and . In general finding these factorizations is probably a harder problem than testing the primality of , so the methods are only applicable in special cases, but are they are interesting nonetheless.
At a high level, the method works by showing that a subgroup of is so large that must be prime. Specifically, a subgroup of order is demonstrated, which implies that is as large as possible, namely the full set of nonzero residues ; if even one element was missing then there would be less than elements in . But means that every positive integer strictly less than is coprime to , and so is prime.
To show that contains a subgroup of size , we find an element whose order is (such an element is called a primitive root). To do this, we show that
and
for all primes which divide . This is enough to show that the order of is ; if the true order was then using Bézout’s identity one would be able to derive and this would contradict (2) for some .
When is prime, there isn’t any known efficient algorithm which is guaranteed to find a primitive root satisfying (1) and (2), but in practice this isn’t a concern. In fact, there are primitive roots (where denotes Euler’s totient function), so if one simply tests if random satisfies (1) and (2) then one should quickly find one which works, and thereby prove that is prime. As mentioned, the real problem with applying this method in practice is finding the primes which divide .
Incidentally, when isn’t prime, this is usually easy to show since condition (1) will often fail to hold, and all primes satisfy (1) for all . However, some composite numbers still satisfy (1) for all . 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” with odd, which is simple to compute.
The method is similar to the method, except it works in the group , where satisfies , i.e., is not a quadratic residue mod . We assume that is odd (otherwise the problem is trivial), so that in practice can be found by computing the Jacobi symbol for multiple values of until one works. Note that when is prime we have , the finite field of size , so there still exist primitive roots which generate . We’ll denote the conjugate of by , so if with , then .
As you might expect in relation to the above, the method finds a subgroup of of size by finding an which has order . Actually, we need something a bit stronger than this; we need to show
and
for all primes which divide . Condition (4) not only implies that , but also that where is any prime divisor of . To see that this, we show the contrapositive. Suppose that is a prime divisor of and that .1 It follows that also divides , and thus divides the gcd in (4), so (4) fails to hold.
Since condition (3) implies that and condition (4) implies that , just like before one knows that has order . In particular, there must be at least elements in .
Toward a contradiction, suppose that (3) and (4) hold and that is not prime. Let be the smallest prime divisor of , so that . As we just saw, has order , so we get the lower bound
However, has elements, and so
which contradicts the lower bound. Thus must in fact be prime.
When is prime, an which satisfies (3) and (4) can be found by taking a primitive root of and raising it to the power , since in this case and will not divide or its conjugate. As mentioned, there isn’t a guaranteed procedure to find a primitive root, but since there are of them, in practice it shouldn’t be too hard to find one; the sticking point is finding the factorization of .
Incidentally, the test is often presented using Lucas sequences rather than . But that’s a topic for another post.