This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
/*
This is a Java Program to Implement Fermat Factorization Algorithm. Fermat’s factorization method, named after Pierre de Fermat, is based on the representation of an odd integer as the difference of two squares: N = a2 – b2. That difference is algebraically factorable as (a + b)(a – b); if neither factor equals one, it is a proper factorization of N.
*/
/**
** Java Program to implement Fermat Factorization Algorithm
**/
importjava.util.Scanner;
publicclassFermatFactorization
{
/** Fermat factor **/
publicvoidFermatFactor(longN)
{
longa=(long)Math.ceil(Math.sqrt(N));
longb2=a*a-N;
while(!isSquare(b2))
{
a++;
b2=a*a-N;
}
longr1=a-(long)Math.sqrt(b2);
longr2=N/r1;
display(r1,r2);
}
/** function to display roots **/
publicvoiddisplay(longr1,longr2)
{
System.out.println("\nRoots = "+r1+" , "+r2);
}
/** function to check if N is a perfect square or not **/