Proof of Fermat's Little Theorem
Theorem 1
Prime numbers and integers that are coprime to each other,
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 is established, then eliminating from both sides would prove Fermat’s little theorem. That is, it suffices to show that two sets and are identical in .
The set has exactly elements as a finite set. Since is coprime to , the remainders when these elements are divided by will be integers between and . If there are no duplicates among those remainders, then will hold. Consider two distinct elements and from . Assuming the remainders of dividing both by are the same implies that holds. Since is coprime to , canceling out from both sides also makes valid. However, within the set, and were integers greater than and less than , thus and are also the same. This contradicts the assumption, indicating that the remainders when different elements are divided by are always distinct. With no duplicates among the remainders, in , holds. Moreover, since is a prime, it is coprime to . Upon eliminating from both sides, the congruence is obtained.
■
Keep in mind the following corollaries as well.
Corollaries
- [1] Inverse: In , the multiplicative inverse of is necessarily given as
- [2] Fermat’s test: If does not hold, then 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 is a composite, 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.
Fermat’s test was able to definitively catch composites such as or and correctly passed prime numbers like . However, it failed to catch Carmichael numbers such as , , , and . To detect Carmichael numbers, one would need to use a method like the Miller-Rabin test.
Silverman. (2012). A Friendly Introduction to Number Theory (4th Edition): p66. ↩︎