Fermat 's Factorization Method | 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
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 ) ;