A test based on Fermat’s Theorem ($a^{n-1} \equiv 1 \pmod n$ if n is prime)
-For a candidate odd integer n (3), consider $(n-1)$ s.t. $n-1=2^kq$ with $k>0$, $q$ odd
That is, we divide (n-1) by 2 until the result is an odd number, for a total of $k$ divisions.
-Next we choose an integer $a$ ($1 < a < n - 1 $).
Then involve computation of the residues modulo n of the following sequence of powers
-$a^q, a^{2q}, ..., a^{2^{k-1}q}$, $a^{2^kq}$
-If $n$ is prime, $a^{(2k-1)q} \equiv a^{n-1} \equiv 1 \pmod n$.
-There may or may not be an earlier element of the sequence that has a residue 1
-If n is prime, there is a smallest value of j ($0<j<k$) such that $a^{2jq}\equiv 1 \pmod n$.
-There are two cases to consider
-Case 1 ($j=0$) : We have $a^{q–1} \equiv 0 \pmod n$
-Case 2 ($1<j<k$) : $(a^{2^jq}-1) \equiv (a^{2^{j-1}q}-1)(a^{2^{j+1}q}+1) \equiv 0 \pmod n$.
Because j is the smallest integer s.t. n divides $(a^{2^jq}-1)$, n divides $(a2^{(j+1)q}+1)$
A test based on Fermat’s Theorem ($a^{n-1} \equiv 1 \pmod n$ if $n$ is prime)
-Algorithm is:
TEST (n) is:
Find integers $k$, $q$, $k > 0$, $q$ odd, so that $(n–1)=2^kq$
Select a random integer a, 1
if $a^q \equiv 1 \pmod n$ then return (“maybe prime");
for j = 0 to k –1 do
if($a^{2^jq} \mod n = n-1$)
then return(" maybe prime ")
return ("composite")