Fermat’s Factorization Method
Last Updated :
07 Jan, 2024
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 – 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++
#include<bits/stdc++.h>
using namespace std;
void FermatFactors( int n)
{
if (n <= 0)
{
cout << "[" << n << "]" ;
return ;
}
if ((n & 1) == 0)
{
cout << "[" << n / 2.0 << "," << 2 << "]" ;
return ;
}
int a = ceil ( sqrt (n)) ;
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 ;
}
int main()
{
FermatFactors(6557);
return 0;
}
|
Java
class GFG
{
static void FermatFactors( int n)
{
if (n <= 0 )
{
System.out.print( "[" + n + "]" );
return ;
}
if ((n & 1 ) == 0 )
{
System.out.print( "[" + n / 2.0 + "," + 2 + "]" );
return ;
}
int a = ( int )Math.ceil(Math.sqrt(n)) ;
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 ;
}
public static void main (String[] args)
{
FermatFactors( 6557 );
}
}
|
Python3
from math import ceil, sqrt
def FermatFactors(n):
if (n< = 0 ):
return [n]
if (n & 1 ) = = 0 :
return [n / 2 , 2 ]
a = ceil(sqrt(n))
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]
print (FermatFactors( 6557 ))
|
C#
using System;
class GFG
{
static void FermatFactors( int n)
{
if (n <= 0)
{
Console.Write( "[" + n + "]" );
return ;
}
if ((n & 1) == 0)
{
Console.Write( "[" + n / 2.0 + "," + 2 + "]" );
return ;
}
int a = ( int )Math.Ceiling(Math.Sqrt(n)) ;
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 ;
}
public static void Main ()
{
FermatFactors(6557);
}
}
|
Javascript
<script>
function FermatFactors(n)
{
if (n <= 0)
{
document.write( "[" + n + "]" );
return ;
}
if ((n & 1) == 0)
{
document.write( "[" + (n / 2.0) + "," + (2) + "]" );
return ;
}
let a = Math.ceil(Math.sqrt(n)) ;
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>
|
Time Complexity : O(sqrt(n))
Space Complexity : O(1)
Share your thoughts in the comments
Please Login to comment...