Fermat’s Last Theorem

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

In this article, we will be talking about Fermat's Last Theorem , also called as Fermat's Great theorem. Stated by Pierre de Fermat in around 1637 in a margin of his copy of Arithmetica. A Nightmare for Mathematicians to prove. This theorem resisted proof for around 358 years until a successful proof was released by Andrew Wiles in 1994.

Table of contents:

  1. Statement of Fermat’s Last Theorem
  2. Proof for n = 4
  3. Algorithm
  4. Conclusion

Statement of Fermat’s Last Theorem

It states that there are no natural numbers x , y and z that satisfies the equation:

xn + yn = zn , where n > 2.

Consider an Example , let's assume n = 4 , then from Fermat's last theorem we can state that there are no 3 natural numbers x , y and z such that
x4 + y4=z4

When considering n to be 1 or 2 , we have infinite possibilities , pythogorean triplets are example of these.
Examples include:
1 + 2 = 3 , with n = 1
32 + 42 = 52 , n = 2

Proof for n = 4

Consider the Equation , where xyz != 0
x4 + y4 = z2
We can consider x2 , y2 and z to be coprime (article linked in References).
As the Solution is in the form of a pythongorean triplet , From its Solution , we know that there exists 2 numbers p and q such that:
x2 = 2pq
y2 = p2 - q2
z = p2 + q2

from the above equations , we have another triplet:
y2 + q2 = p2
For this equation , we have two numbers a and b such that:
q = 2ab
y = a2 - b2
p = a2 + b2

Combining Equations from above , we get
x2 = 2pq = 2(a2 + b2)(2ab) = (4ab)(a2 + b2)

We can prove that ab and a2 + b2 are relative primes , hence we can consider them squares.
Now there exists P such that P2 = a2 + b2 , which is less than z2 .

With this we reach infinite descent as P2 = a2 + b2 = p , which is itself less than z = p2 + q2 , which is less than z2 .

As the existence of a solution to the original equation leads necessarily to the existence of another smaller square with same properties , the case for n = 4 can be stated as a corollary to this.
x4 + y4 = (z2)2 has no solution when xyz != 0


Consider that we're given a limit until which we need to check if there exists a counter example for Fermat's Last Theorem.

Algorithm checkUntilLimit(Limit , n){
    if n < 3 {
        write "Invalid Value of n"
    for(let a <- 1 , a <= limit , a <- a+ )
        for(let b <- a , b <= limit , b <- b + 1)
            let sum <- power(a , n) + power(b , n)
            let c <- power(sum , 1.0/n)
            let d <- (integer)power((integer)c , n)
            if c == d
                write "Counter Example Found , Fermat's Last Theorem Defeated"
    write " Can't Find a Counter Example for Fermat's Last Theorem"

if we pass simple values into our algorithm , say these values for limit and n were (4 , 3) , then the output would be

Can't Find a Counter Example for Fermat's Last Theorem

In case we pass values that are very large which may cause integer overflow , this may cause the algorithm to print that it found a counter example , but that is not true , due to overflow in the variables , it thinks that it found the two values to be same , but as we know that isn't the case. To overcome this issue we may use BigInteger if using Java or Other Language equivalent of BigInteger.


Fermat's Last Theorem resisted proof's for around 350 odd years , but this resistance proved very prosperous for mathematics.Fermat's Last theorem might not have that many real world applications but when people were trying to prove this theorem , they tried various methods which led to the discovery of many important mathematical topics , these very same topics found applications in computer science, the most important application being Cryptography. Wiles proof for this theorem proved to be a crucial step in solving another theorem called as modularity theorem also known as Taniyama-Shimura Conjecture.

With this article at OpenGenus, you must have the complete idea of Fermat’s Last Theorem.