logo

Proof of Fermat's Little Theorem 📂Number Theory

Proof of Fermat's Little Theorem

Theorem 1

Prime numbers pp and integers aa that are coprime to each other, ap11(modp)a^{p-1} \equiv 1 \pmod{p}

Explanation

Fermat’s little theorem is a straightforward yet highly influential theorem used in a variety of fields. Although there is a generalized theorem by Euler, Fermat’s little theorem is often sufficient. It is particularly essential in fields like cryptography that extensively deal with exponentiation in finite fields.

Proof

Strategy: The proof is brutishly simple yet not trivial. If a2a(p1)a(p1)!(modp) a \cdot 2a \cdot \cdots \cdot (p-1)a \equiv (p-1)! \pmod{p} is established, then eliminating (p1)!(p-1)! from both sides would prove Fermat’s little theorem. That is, it suffices to show that two sets {a,2a,,(p1)a}\left\{ a,2a,\cdots , (p-1)a \right\} and {1,2,,(p1)}\left\{1,2,\cdots ,(p-1) \right\} are identical in (modp)\pmod{p}.


The set {a,2a,,(p1)a}\left\{ a,2a,\cdots , (p-1)a \right\} has exactly (p1)(p-1) elements as a finite set. Since aa is coprime to pp, the remainders when these elements are divided by pp will be integers between 11 and (p1)(p-1). If there are no duplicates among those remainders, then {a,2a,,(p1)a}={1,2,,(p1)} \left\{ a,2a,\cdots , (p-1)a \right\} = \left\{1,2,\cdots ,(p-1) \right\} will hold. Consider two distinct elements iaia and jaja from {a,2a,,(p1)a}\left\{ a,2a,\cdots , (p-1)a \right\}. Assuming the remainders of dividing both by pp are the same implies that iaja(modp)ia \equiv ja \pmod{p} holds. Since aa is coprime to pp, canceling out aa from both sides also makes ij(modp)i \equiv j \pmod{p} valid. However, within the set, ii and jj were integers greater than 00 and less than pp, thus iaia and jaja are also the same. This contradicts the assumption, indicating that the remainders when different elements are divided by pp are always distinct. With no duplicates among the remainders, in (modp)\pmod{p}, {a,2a,,(p1)a}={1,2,,(p1)} \left\{ a,2a,\cdots , (p-1)a \right\} = \left\{1,2,\cdots ,(p-1) \right\} holds. Moreover, since pp is a prime, it is coprime to (p1)!(p-1)!. (p1)!ap1(p1)!(modp) (p-1)! a^{p-1} \equiv (p-1)! \pmod{p} Upon eliminating (p1)!(p-1)! from both sides, the congruence ap11(modp)a^{p-1} \equiv 1 \pmod{p} is obtained.

Keep in mind the following corollaries as well.

Corollaries

  • [1] Inverse: In (modp)\pmod{p}, the multiplicative inverse of aa is necessarily given as a1ap2(modp) a^{-1} \equiv a^{p-2} \pmod{p}
  • [2] Fermat’s test: If ana(modn)a^n \equiv a \pmod{n} does not hold, then nn is a composite number.

Determining whether a number is prime can be challenging, but identifying a composite number is relatively easier. Note that the converse of Fermat’s test is not true. A counterexample that demonstrates the failure of the converse is Carmichael numbers. For instance, although 561=31117561=3 \cdot 11 \cdot 17 is a composite, a561a(mod561)a^{561} \equiv a \pmod{561} always holds.

Code

The following is an implementation of Fermat’s test in R, utilizing the method of exponentiation by squaring.

FPM<-function(base,power,mod) #It is equal to (base^power)%%mod
{
  i<-0
  if (power<0) {
    while((base*i)%%mod != 1) {i=i+1}
    base<-i
    power<-(-power)}
  
  if (power==0) {return(1)}
  if (power==1) {return(base%%mod)}
  
  n<-0
  while(power>=2^n) {n=n+1}
  
  A<-rep(1,n)
  A[1]=base
  
  for(i in 1:(n-1)) {A[i+1]=(A[i]^2)%%mod}
  
  for(i in n:1) {
    if(power>=2^(i-1)) {power=power-2^(i-1)}
    else {A[i]=1} }
  
  for(i in 2:n) {A[1]=(A[1]*A[i])%%mod}
  
  return(A[1])
}
 
fermat.test<-function(n)
{
  for(i in 2:(n-1))  {if( i!=FPM(i,n,n) ) {return(paste(i,"is a Fermat witness!"))}}
  paste(n,"passes the Fermat test!")
}
 
fermat.test(121)
fermat.test(341)    #Almost composite yields fermat witness 2, but 341=11*31 doesn't.
fermat.test(561)    #Carmicheal number 561 = 3*11*17
fermat.test(1031)    #1031 is a prime
fermat.test(1105)    #Carmicheal number 1105 = 5*13*17
fermat.test(1729)    #Carmicheal number 1729 = 7*13*19
fermat.test(41041)    #Carmicheal number 41041 = 7*11*13*41

Below is the result of executing the code.

20190304\_085849.png

Fermat’s test was able to definitively catch composites such as 121121 or 341341 and correctly passed prime numbers like 10311031. However, it failed to catch Carmichael numbers such as 561561, 11051105, 17291729, and 4104141041. To detect Carmichael numbers, one would need to use a method like the Miller-Rabin test.


  1. Silverman. (2012). A Friendly Introduction to Number Theory (4th Edition): p66. ↩︎