Fermat’s Factorization Method

10 Jun, 2021
Fermat’s Factorization method is based on the representation of an odd integer as the difference of two squares. For an integer n, we want a and b such as:  
n = a2 - b2 = (a+b)(a-b) 

where (a+b) and (a-b) are
the factors of the number n
Example:  
Input: n = 6557
Output: [79,83]
Explanation: 
For the above value, 
the first try for a is ceil value 
of square root of 6557, which is 81.

Then, 
b2 = 812 - 6557 = 4,
as it is a perfect square.
So, b = 2

So, the factors of 6557 are:  
(a - b) = 81 -2  = 79 & 
(a + b) = 81 + 2 = 83.
 
Approach :  
If n = pq is a factorization of n into two positive integers, Then, since n is odd, so p and q are both odd.Let, a = 1/2 * (p+q) and b = 1/2 * (q-p).Since a and b are both integers, then p = (a – b) and q = (a + b).So, n = pq = (a – b)(a + b) = a2 – b2In case of prime number, we go back until b = 1 in as one factor is 1 for a prime number.A while loop ensures this operation
If n = pq is a factorization of n into two positive integers, Then, since n is odd, so p and q are both odd.
Let, a = 1/2 * (p+q) and b = 1/2 * (q-p).
Since a and b are both integers, then p = (a – b) and q = (a + b).
So, n = pq = (a – b)(a + b) = a2 – b2
In case of prime number, we go back until b = 1 in as one factor is 1 for a prime number.
A while loop ensures this operation
Below is the implementation of the above approach 
C++
Java
Python3
C#
Javascript
// C++ implementation of fermat's factorization#include<bits/stdc++.h> using namespace std;     // This function finds the value of a and b    // and returns a+b and a-b    void FermatFactors(int n)    {                 // since fermat's factorization applicable        // for odd positive integers only        if(n <= 0)        {            cout << "[" << n << "]";            return;        }             // check if n is a even number        if((n & 1) == 0)        {            cout << "[" << n / 2.0 << "," << 2 << "]";            return;        }                     int a = ceil(sqrt(n)) ;             // if n is a perfect root,        // then both its square roots are its factors        if(a * a == n)        {            cout << "[" << a << "," << a << "]";            return;        }        int b;        while(true)        {            int b1 = a * a - n ;            b = (int)sqrt(b1) ;                         if(b * b == b1)                break;            else                a += 1;        }        cout << "[" << (a - b) << "," << (a + b) << "]" ;        return;    }             // Driver Code    int main()    {        FermatFactors(6557);        return 0;    } // This code is contributed by AnkitRai01
// Java implementation of fermat's factorizationclass GFG{         // This function finds the value of a and b    // and returns a+b and a-b    static void FermatFactors(int n)    {                 // since fermat's factorization applicable        // for odd positive integers only        if(n <= 0)        {            System.out.print("["+ n + "]");            return;        }             // check if n is a even number        if((n & 1) == 0)        {            System.out.print("[" + n / 2.0 + "," + 2 + "]");            return;        }                     int a = (int)Math.ceil(Math.sqrt(n)) ;             // if n is a perfect root,        // then both its square roots are its factors        if(a * a == n)        {            System.out.print("[" + a + "," + a + "]");            return;        }        int b;        while(true)        {            int b1 = a * a - n ;            b = (int)(Math.sqrt(b1)) ;                         if(b * b == b1)                break;            else                a += 1;        }        System.out.print("[" + (a - b) +"," + (a + b) + "]" );        return;    }             // Driver Code    public static void main (String[] args)    {        FermatFactors(6557);    }} // This code is contributed by AnkitRai01
# Python 3 implementation of fermat's factorization from math import ceil, sqrt #This function finds the value of a and b#and  returns a+b and a-bdef FermatFactors(n):    # since fermat's factorization applicable   # for odd positive integers only    if(n<= 0):        return [n]      # check if n is a even number    if(n & 1) == 0:         return [n / 2, 2]             a = ceil(sqrt(n))     #if n is a perfect root,    #then both its square roots are its factors    if(a * a == n):        return [a, a]     while(True):        b1 = a * a - n        b = int(sqrt(b1))        if(b * b == b1):            break        else:            a += 1    return [a-b, a + b]     # Driver Codeprint(FermatFactors(6557))
// C# implementation of fermat's factorizationusing System; class GFG{         // This function finds the value of a and b    // and returns a+b and a-b    static void FermatFactors(int n)    {                 // since fermat's factorization applicable        // for odd positive integers only        if(n <= 0)        {            Console.Write("["+ n + "]");            return;        }             // check if n is a even number        if((n & 1) == 0)        {            Console.Write("[" + n / 2.0 + "," + 2 + "]");            return;        }                     int a = (int)Math.Ceiling(Math.Sqrt(n)) ;             // if n is a perfect root,        // then both its square roots are its factors        if(a * a == n)        {            Console.Write("[" + a + "," + a + "]");            return;        }        int b;        while(true)        {            int b1 = a * a - n ;            b = (int)(Math.Sqrt(b1)) ;                         if(b * b == b1)                break;            else                a += 1;        }        Console.Write("[" + (a - b) +"," + (a + b) + "]" );        return;    }             // Driver Code    public static void Main ()    {        FermatFactors(6557);    }} // This code is contributed by AnkitRai01
<script>     // JavaScript implementation of fermat's factorization         // This function finds the value of a and b    // and returns a+b and a-b    function FermatFactors(n)    {                   // since fermat's factorization applicable        // for odd positive integers only        if(n <= 0)        {            document.write("["+ n + "]");            return;        }               // check if n is a even number        if((n & 1) == 0)        {            document.write("[" + (n / 2.0) + "," + (2) + "]");            return;        }                       let a = Math.ceil(Math.sqrt(n)) ;               // if n is a perfect root,        // then both its square roots are its factors        if(a * a == n)        {            document.write("[" + a + "," + a + "]");            return;        }        let b;        while(true)        {            let b1 = a * a - n ;            b = parseInt(Math.sqrt(b1), 10);                           if(b * b == b1)                break;            else                a += 1;        }        document.write("[" + (a - b) +", " + (a + b) + "]");        return;    }         FermatFactors(6557);     </script>
[79, 83]
 
ankthon
suresh07
number-theory
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Find minimum number of coins that make a given value
Algorithm to solve Rubik's Cube
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number