 Open in App
Not now

# Fermat’s Factorization Method

• Difficulty Level : Hard
• Last Updated : 05 Mar, 2023

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 :

1. If n = pq is a factorization of n into two positive integers, Then, since n is odd, so p and q are both odd.
2. Let, a = 1/2 * (p+q) and b = 1/2 * (q-p).
3. Since a and b are both integers, then p = (a – b) and q = (a + b).
4. So, n = pq = (a – b)(a + b) = a2 – b2
5. In case of prime number, we go back until b = 1 in as one factor is 1 for a prime number.
6. A while loop ensures this operation

Below is the implementation of the above approach

## C++

 `// C++ implementation of fermat's factorization``#include` `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

 `// Java implementation of fermat's factorization``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``)``        ``{``            ``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`

## Python3

 `# 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-b``def` `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 Code``print``(FermatFactors(``6557``))`

## C#

 `// C# implementation of fermat's factorization``using` `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`

## Javascript

 ``

Output:

`[79, 83]`

Time Complexity :   O(sqrt(n))

Space Complexity : O(1)

My Personal Notes arrow_drop_up