Fermat 's Factorization Method | Java implementation of fermat 's factorization ; This function finds the value of a and b and returns a + b and a - b ; since fermat 's factorization applicable  for odd positive integers only ; check if n is a even number ; if n is a perfect root , then both its square roots are its factors ; Driver Code
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 ) ; } }