Euler 's Factorization method | Java program to implement Eulers Factorization algorithm ;  ; Recursive function to return gcd of a and b ; Function to return N as the sum of two squares in two possible ways ; Iterate a loop from 1 to Math . sqrt ( n ) ; If i * i is square check if there exists another integer such that h is a perfect square and i * i + h = n ; If h is perfect square ; Store in the sorted way ; If there is already a pair check if pairs are equal or not ; Insert the first pair ; If two pairs are found ; Function to find the factors ; Get pairs where a ^ 2 + b ^ 2 = n ; Number cannot be represented as sum of squares in two ways ; Assign a , b , c , d ; Swap if a < c because if a - c < 0 , GCD cant be computed . ; Compute the values of k , h , l , m using the formula mentioned in the approach ; Print the values of a , b , c , d and k , l , m , h ; Printing the factors ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void sumOfSquares ( int n , Vector < pair > vp ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { int h = n - i * i , h1 = ( int ) Math . sqrt ( h ) ; if ( h1 * h1 == h ) { int a = Math . max ( h1 , i ) , b = Math . min ( h1 , i ) ; if ( vp . size ( ) == 1 && a != vp . get ( 0 ) . first ) vp . add ( new pair ( a , b ) ) ; if ( vp . size ( ) == 0 ) vp . add ( new pair ( a , b ) ) ; if ( vp . size ( ) == 2 ) return ; } } } static void findFactors ( int n ) { Vector < pair > vp = new Vector < > ( ) ; sumOfSquares ( n , vp ) ; if ( vp . size ( ) != 2 ) System . out . print ( " Factors ▁ Not ▁ Possible " ) ; int a , b , c , d ; a = vp . get ( 0 ) . first ; b = vp . get ( 0 ) . second ; c = vp . get ( 1 ) . first ; d = vp . get ( 1 ) . second ; if ( a < c ) { int t = a ; a = c ; c = t ; t = b ; b = d ; d = t ; } int k , h , l , m ; k = __gcd ( a - c , d - b ) ; h = __gcd ( a + c , d + b ) ; l = ( a - c ) / k ; m = ( d - b ) / k ; System . out . print ( " a ▁ = ▁ " + a + " TABSYMBOL TABSYMBOL ( A ) ▁ a ▁ - ▁ c ▁ = ▁ " + ( a - c ) + " TABSYMBOL TABSYMBOL k ▁ = ▁ gcd [ A , ▁ C ] ▁ = ▁ " + k + "NEW_LINE"); System . out . print ( " b ▁ = ▁ " + b + " TABSYMBOL TABSYMBOL ( B ) ▁ a ▁ + ▁ c ▁ = ▁ " + ( a + c ) + " TABSYMBOL TABSYMBOL h ▁ = ▁ gcd [ B , ▁ D ] ▁ = ▁ " + h + "NEW_LINE"); System . out . print ( " c ▁ = ▁ " + c + " TABSYMBOL TABSYMBOL ( C ) ▁ d ▁ - ▁ b ▁ = ▁ " + ( d - b ) + " TABSYMBOL TABSYMBOL l ▁ = ▁ A / k ▁ = ▁ " + l + "NEW_LINE"); System . out . print ( " d ▁ = ▁ " + d + " TABSYMBOL TABSYMBOL ( D ) ▁ d ▁ + ▁ b ▁ = ▁ " + ( d + b ) + " TABSYMBOL TABSYMBOL m ▁ = ▁ c / k ▁ = ▁ " + m + "NEW_LINE"); if ( k % 2 == 0 && h % 2 == 0 ) { k = k / 2 ; h = h / 2 ; System . out . print ( " Factors ▁ are : ▁ " + ( ( k ) * ( k ) + ( h ) * ( h ) ) + " ▁ " + ( l * l + m * m ) + "NEW_LINE"); } else { l = l / 2 ; m = m / 2 ; System . out . print ( " Factors ▁ are : ▁ " + ( ( l ) * ( l ) + ( m ) * ( m ) ) + " ▁ " + ( k * k + h * h ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 100000 ; findFactors ( n ) ; } }