Fermat 's Factorization Method | C ++ 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
#include <bits/stdc++.h> NEW_LINE 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 ; }