The Wayback Machine - https://web.archive.org/web/20240118174201/https://www.geeksforgeeks.org/fermats-last-theorem/
Open In App
Related Articles

Fermat’s Last Theorem

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

According to Fermat’s Last Theorem, no three positive integers a, b, c satisfy the equation, a^n + b^n = c^n    for any integer value of n greater than 2. For n = 1 and n = 2, the equation have infinitely many solutions.
 

Some solutions for n = 1 are,
 2 + 3 = 5
 7 + 13 = 20
 5 + 6 = 11
 10 + 9 = 19

Some solutions for n = 2 are,
5^2 + 12^2 = 13^2     3^2 + 4^2 = 5^2    8^2 + 15^2 = 17^2    9^2 + 40^2 = 41^2

 

C++




// C++ program to verify fermat's last theorem
// for a given range and n.
#include <bits/stdc++.h>
using namespace std;
 
void testSomeNumbers(int limit, int n)
{
   if (n < 3)
     return;
 
   for (int a=1; a<=limit; a++)
     for (int b=a; b<=limit; b++)
     {
         // Check if there exists a triplet
         // such that a^n + b^n = c^n
         int pow_sum = pow(a, n) + pow(b, n);
         double c = pow(pow_sum, 1.0/n);
         int c_pow = pow((int)c, n);
         if (c_pow == pow_sum)
         {
             cout << "Count example found";
             return;
         }
     }
 
     cout << "No counter example within given"
            " range and data";
}
 
// driver code
int main()
{
    testSomeNumbers(10, 3);
    return 0;
}


Java




// Java program to verify fermat's last theorem
// for a given range and n.
import java.io.*;
 
class GFG
{
    static void testSomeNumbers(int limit, int n)
    {
        if (n < 3)
            return;
         
        for (int a = 1; a <= limit; a++)
            for (int b = a; b <= limit; b++)
            {
                // Check if there exists a triplet
                // such that a^n + b^n = c^n
                int pow_sum = (int)(Math.pow(a, n)
                               + Math.pow(b, n));
                double c = Math.pow(pow_sum, 1.0 / n);
                int c_pow = (int)Math.pow((int)c, n);
                if (c_pow == pow_sum)
                {
                    System.out.println("Count example found");
                    return;
                }
            }
         
            System.out.println("No counter example within given"+
                               " range and data");
    }
     
    // Driver code
    public static void main (String[] args)
    {
        testSomeNumbers(12, 5);
     
    }
}
 
// This code is contributed by vt_m.


Python3




# Python3 program to verify fermat's last
# theorem for a given range and n.
 
def testSomeNumbers(limit, n) :
 
    if (n < 3):
        return
     
    for a in range(1, limit + 1):
        for b in range(a, limit + 1):
         
            # Check if there exists a triplet
            # such that a^n + b^n = c^n
            pow_sum = pow(a, n) + pow(b, n)
            c = pow(pow_sum, 1.0 / n)
            c_pow = pow(int(c), n)
             
            if (c_pow == pow_sum):
                print("Count example found")
                return
    print("No counter example within given range and data")
 
# Driver code
testSomeNumbers(10, 3)
 
# This code is contributed by Smitha Dinesh Semwal.


C#




// C# program to verify fermat's last theorem
// for a given range and n.
using System;
 
class GFG {
     
    static void testSomeNumbers(int limit, int n)
    {
        if (n < 3)
            return;
         
        for (int a = 1; a <= limit; a++)
            for (int b = a; b <= limit; b++)
            {
                 
                // Check if there exists a triplet
                // such that a^n + b^n = c^n
                int pow_sum = (int)(Math.Pow(a, n)
                                + Math.Pow(b, n));
                double c = Math.Pow(pow_sum, 1.0 / n);
                int c_pow = (int)Math.Pow((int)c, n);
                 
                if (c_pow == pow_sum)
                {
                    Console.WriteLine("Count example found");
                    return;
                }
            }
         
            Console.WriteLine("No counter example within"
                                + " given range and data");
    }
     
    // Driver code
    public static void Main ()
    {
        testSomeNumbers(12, 3);
     
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to verify fermat's
// last theorem for a given range
//and n.
 
function testSomeNumbers($limit, $n)
{
    if ($n < 3)
     
    for($a = 1; $a <= $limit; $a++)
        for($b = $a; $b <= $limit; $b++)
    {
         
        // Check if there exists a triplet
        // such that a^n + b^n = c^n
        $pow_sum = pow($a, $n) + pow($b, $n);
         
        $c = pow($pow_sum, 1.0 / $n);
        $c_pow = pow($c, $n);
        if ($c_pow != $pow_sum)
        {
            echo "Count example found";
            return;
        }
    }
 
    echo "No counter example within ".
              "given range and data";
}
 
    // Driver Code
    testSomeNumbers(10, 3);
 
// This code is contributed by m_kit
?>


Javascript




<script>
 
// JavaScript program to verify fermat's last theorem
// for a given range and n.
 
    function testSomeNumbers(limit, n)
    {
        if (n < 3)
            return;
           
        for (let a = 1; a <= limit; a++)
            for (let b = a; b <= limit; b++)
            {
                // Check if there exists a triplet
                // such that a^n + b^n = c^n
                let pow_sum = (Math.pow(a, n)
                               + Math.pow(b, n));
                let c = Math.pow(pow_sum, 1.0 / n);
                let c_pow = Math.pow(Math.round(c), n);
                if (c_pow == pow_sum)
                {
                    document.write("Count example found");
                    return;
                }
            }
           
            document.write("No counter example within given"+
                               " range and data");
    }
  
 
// Driver Code
 
        testSomeNumbers(12, 5);
           
</script>


Output: 

No counter example within given range and data

 

Time Complexity: O(m2logn) , where m is the limit
Auxiliary Space: O(1)

Please suggest if someone has a better solution which is more efficient in terms of space and time.


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 23 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials