Euler 's Factorization method | C # 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
using System ; using System . Collections . Generic ; class GFG { public class pair { public 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 , List < 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 . Count == 1 && a != vp [ 0 ] . first ) vp . Add ( new pair ( a , b ) ) ; if ( vp . Count == 0 ) vp . Add ( new pair ( a , b ) ) ; if ( vp . Count == 2 ) return ; } } } static void findFactors ( int n ) { List < pair > vp = new List < pair > ( ) ; sumOfSquares ( n , vp ) ; if ( vp . Count != 2 ) Console . Write ( " Factors ▁ Not ▁ Possible " ) ; int a , b , c , d ; a = vp [ 0 ] . first ; b = vp [ 0 ] . second ; c = vp [ 1 ] . first ; d = vp [ 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 ; Console . Write ( " a ▁ = ▁ " + a + " TABSYMBOL TABSYMBOL ( A ) ▁ a ▁ - ▁ c ▁ = ▁ " + ( a - c ) + " TABSYMBOL TABSYMBOL k ▁ = ▁ gcd [ A , ▁ C ] ▁ = ▁ " + k + " STRNEWLINE " ) ; Console . Write ( " b ▁ = ▁ " + b + " TABSYMBOL TABSYMBOL ( B ) ▁ a ▁ + ▁ c ▁ = ▁ " + ( a + c ) + " TABSYMBOL TABSYMBOL h ▁ = ▁ gcd [ B , ▁ D ] ▁ = ▁ " + h + " STRNEWLINE " ) ; Console . Write ( " c ▁ = ▁ " + c + " TABSYMBOL TABSYMBOL ( C ) ▁ d ▁ - ▁ b ▁ = ▁ " + ( d - b ) + " TABSYMBOL TABSYMBOL l ▁ = ▁ A / k ▁ = ▁ " + l + " STRNEWLINE " ) ; Console . Write ( " d ▁ = ▁ " + d + " TABSYMBOL TABSYMBOL ( D ) ▁ d ▁ + ▁ b ▁ = ▁ " + ( d + b ) + " TABSYMBOL TABSYMBOL m ▁ = ▁ c / k ▁ = ▁ " + m + " STRNEWLINE " ) ; if ( k % 2 == 0 && h % 2 == 0 ) { k = k / 2 ; h = h / 2 ; Console . Write ( " Factors ▁ are : ▁ " + ( ( k ) * ( k ) + ( h ) * ( h ) ) + " ▁ " + ( l * l + m * m ) + " STRNEWLINE " ) ; } else { l = l / 2 ; m = m / 2 ; Console . Write ( " Factors ▁ are : ▁ " + ( ( l ) * ( l ) + ( m ) * ( m ) ) + " ▁ " + ( k * k + h * h ) + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int n = 100000 ; findFactors ( n ) ; } }