# 6. Primality testing

```Primality-testing
There is an important problem in Number Theory:

How do we verify if a positive integer n is prime?
A “simple” way is to check whether n is divisible by some prime p  n , but it is practically
impossible if n is large (even if we have computer). Therefore, a more efficient way should be
found.
Fermat’s Little Theorem says that if p is prime then a p 1  1 (mod p) for all integers a with
p | a . We ask if the converse is also true. For simplicity, we start with the case a = 2.
Question.
If an odd integer n  1 satisfies 2n1  1 (mod n) , must n be a prime?
No! One has 2340  1 (mod 341) but 341  11 31 is not a prime.
This lead to the following definition:
Definition. A composite number n is said to be a 2-pseudoprime if 2n1  1 (mod n) .
More generally, let b  1 be integer, a composite number n is said to be b-pseudoprime if
bn1  1 (mod n) .
If there is no 2-pseudoprime then we have a simple way to test whether a positive integer is prime.
Unfortunately, 2-pseudoprime exists! The smallest one is 341.
We will hope there are only finitely many 2-pseudoprimes (then we still have a primality-test).
The following theorem discourage us:
Theorem.
If n is a 2-pseudoprime then m  2n  1 is also a 2-pseudoprime.
Proof. Clearly m is composite. We must show 2m1  1 (mod m) . Equivalently, we need to
show 2m1  1 is divisible by 2n  1 . It suffices to prove n | m  1 .
Since n is 2-pseudoprime, we have 2n1  1 (mod n) , therefore n | 2n  1 .
So, m  1  2n  2  2(2n1  1) which is divisible by n. This completes the proof.
Corollary.
There are infinitely many 2-pseudoprimes.
Exercise.
Show that the Mersenne numbers M p  2 p  1 (p is prime) and the Fermat
numbers Fn  22  1 ( n  0 ) are either prime or 2-pseudoprime.
n
When we have an integer n  1 in hand and want to know if it is prime, we first check if n
satisfies 2n  2 (mod n) . If n fails, then we conclude that n is composite. However, if n passes,
then we make no conclusion and need to go on trying the 3-test, 5-test, ... etc.
Page 1 of 2
Note that if n passes both a-test and b-test then n passes ab-test as well. This explains why we
only use p-test where p is prime.
It can be prove that there are infinitely many b-pseudoprimes for each integer b  1. We then ask:
Question.
Are there finitely many primes pi ’s making
S pi   ? Here S pi denotes the
set of all integers n  1 satisfying pi  pi (mod n) .
n
Note that if the answer is yes, then an integer n  1 is prime if and only if n passes pi -test for
each index i (that means, pi  pi (mod n) for each i). Again, we are disappointed!
n
No! One has b561  b (mod 561) for all integers b  1.
A composite number with this property is called Carmichael number. The smallest Carmichael
number is 561. It was prove in 1992 by Alford, Granville and Pomerance that there are infinitely
many Carmichael numbers.
Exercise.
Suppose n is product of distinct primes. If p  1| n  1 for each prime p divides n,
then n is either a prime or a Carmichael number.
Exercise.
Let b  1 be integer and p a prime not dividing b(b2  1) . Prove that n 
b2 p  1
b2  1
is a b-pseudoprime. (As a result, there are infinitely many b-pseudoprimes!)
Miller’s test. Suppose n  1 is odd and b is a positive integer coprime with n. Assume also that
bn1  1 (mod n) . (Otherwise, n must be composite and we have nothing to do.)
We write n  1  2 s t , where s 
s1
If b2
s1
If b2
and t is odd.
t
 1, 1(mod n) , then n must be composite.
t
 1(mod n) , we can go on to calculate b2
s2
t
(mod n) . In this test, we make no
conclusion only when b2 t  1(mod n) for some 0  k  s or bt  1 (mod n) .
k
More precisely, we say that n passes the Miller’s test if
k
(1) b2 t  1(mod n) for some 0  k  s , OR
(2) bt  1 (mod n) .
If n fails the Miller’s test, then n must be composite. If n passes the test, we make no conclusion.
Exercise.
Show that any prime p passes the Miller’s test.
Page 2 of 2
```