Given a number **N**, the task is to find the factors of N.

**Examples:**

Input:N = 1000009

Output:293 3413

Explanation:

293 * 3413 = 1000009

Input:N = 100000

Output:800 125

Explanation:

800 * 125 = 100000

**Euler’s Factorization method:** Euler’s factorization method works on the principle that all the numbers N which can be written as the sum of two powers in two different ways can be factored into two numbers, (i.e) **N = A ^{2} + B^{2} = C^{2} + D^{2}** where A != C and A != D, then there exists two factors for N.

**Working of the algorithm:** Let N be the number for which we need to find the factors.

- So, initially, we need to find two ways to represent N as the sum of powers of two numbers.
N = A

^{2}+ B^{2}N = C^{2}+ D^{2}Therefore, N = A^{2}+ B^{2}= C^{2}+ D^{2} - Now, the algebraic operations are performed on the above equation to convert the equations as:
N = A

^{2}+ B^{2}= C^{2}+ D^{2}-> N = A^{2}- C^{2}= D^{2}- B^{2}-> N = (A - C)(A + C) = (D - B)(D + B) - Let K be the GCD of (A – C) and (D – B). So,
A - C = K * L D - B = K * M where GCD(L, M) is 1.

- Clearly, L = (A – C) / K and M = (D – B)/K. On substituting this in the initial equation:
N = K * L * (A + C) = K * M * (D + B) -> L * (A + C) = M * (D + B) ->l (A + C)/(D + B) = M/L

- Therefore:
(A + C) = M * H (D + B) = L * H where, H = gcd((A + C), (D + B))

- Let Q = (K
^{2}+ H^{2})(L^{2}+ M^{2}).-> ((KL)

^{2}+ (KM)^{2}+ (HL)^{2}+ (HM)^{2}) -> ((A - C)^{2}+ (D - B)^{2}+ (D + B)^{2}+ (A + C)^{2}) -> ((2 * A)^{2}+ (2 * B)^{2}+ (2 * C)^{2}+ (2 * D)^{2}) -> 4 * N - Therefore,
N = ((K/2)

^{2}+ (H/2)^{2})(L^{2}+ M^{2})Such that there exists a pair K and H which are both even numbers.

Let’s visualize the above approach by taking an example. Let N = 221.

- 221 = 11
^{2}+ 10^{2}= 5^{2}+ 14^{2} - From the above equation:
A = 11 - 5 = 6 B = 11 + 5 = 15 C = 14 - 10 = 4 D = 14 + 10 = 24

- Therefore, the above values can be used to compute the values of K, H, L and M.
K = GCD(6, 4) = 2 H = GCD(16, 24) = 8 L = GCD(6, 24) = 3 M = GCD(16, 4) = 2

- Therefore:
221 = ((2/2)

^{2}+ (8/2)^{2}) * (3^{2}+ 2^{2}) 221 = 17 * 13

**Approach:** In order to implement the above approach, the following steps are computed:

- Find the sum of squares by iterating a loop from 1 to sqrt(N) because no factor exists between [sqrt(N), N] apart from N and find two pairs whose sum of squares is equal to N.
- Store the values in A, B, C, D.
- Find the values of K, H, L and M using the formula mentioned in the above approach.
- Use the values of K, H, L and M to find the factors. Check the pair where both the numbers is even and divide them in half and find the factors.

Below is the implementation of the above approach:

`// C++ program to implement Eulers ` `// Factorization algorithm ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return N as the sum of ` `// two squares in two possible ways ` `void` `sumOfSquares(` `int` `n, vector<pair<` `int` `, ` `int` `> >& vp) ` `{ ` ` ` `// Iterate a loop from 1 to sqrt(n) ` ` ` `for` `(` `int` `i = 1; i <= ` `sqrt` `(n); i++) { ` ` ` ` ` `// If i*i is square check if there ` ` ` `// exists another integer such that ` ` ` `// h is a perfect square and i*i + h = n ` ` ` `int` `h = n - i * i, h1 = ` `sqrt` `(h); ` ` ` ` ` `// If h is perfect square ` ` ` `if` `(h1 * h1 == h) { ` ` ` ` ` `// Store in the sorted way ` ` ` `int` `a = max(h1, i), b = min(h1, i); ` ` ` ` ` `// If there is already a pair ` ` ` `// check if pairs are equal or not ` ` ` `if` `(vp.size() == 1 && a != vp[0].first) ` ` ` `vp.push_back(make_pair(a, b)); ` ` ` ` ` `// Insert the first pair ` ` ` `if` `(vp.size() == 0) ` ` ` `vp.push_back(make_pair(a, b)); ` ` ` ` ` `// If two pairs are found ` ` ` `if` `(vp.size() == 2) ` ` ` `return` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to find the factors ` `void` `findFactors(` `int` `n) ` `{ ` ` ` ` ` `// Get pairs where a^2 + b^2 = n ` ` ` `vector<pair<` `int` `, ` `int` `> > vp; ` ` ` `sumOfSquares(n, vp); ` ` ` ` ` `// Number cannot be represented ` ` ` `// as sum of squares in two ways ` ` ` `if` `(vp.size() != 2) ` ` ` `cout << ` `"Factors Not Possible"` `; ` ` ` ` ` `// Assign a, b, c, d ` ` ` `int` `a, b, c, d; ` ` ` ` ` `a = vp[0].first; ` ` ` `b = vp[0].second; ` ` ` ` ` `c = vp[1].first; ` ` ` `d = vp[1].second; ` ` ` ` ` `// Swap if a < c because ` ` ` `// if a - c < 0, ` ` ` `// GCD cant be computed. ` ` ` `if` `(a < c) { ` ` ` `int` `t = a; ` ` ` `a = c; ` ` ` `c = t; ` ` ` `t = b; ` ` ` `b = d; ` ` ` `d = t; ` ` ` `} ` ` ` ` ` `// Compute the values of k, h, l, m ` ` ` `// using the formula mentioned ` ` ` `// in the approach ` ` ` `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; ` ` ` ` ` `// Print the values of a, b, c, d ` ` ` `// and k, l, m, h ` ` ` `cout << ` `"a = "` `<< a ` ` ` `<< ` `"\t\t(A) a - c = "` `<< (a - c) ` ` ` `<< ` `"\t\tk = gcd[A, C] = "` ` ` `<< k << endl; ` ` ` ` ` `cout << ` `"b = "` `<< b ` ` ` `<< ` `"\t\t(B) a + c = "` `<< (a + c) ` ` ` `<< ` `"\t\th = gcd[B, D] = "` ` ` `<< h << endl; ` ` ` ` ` `cout << ` `"c = "` `<< c ` ` ` `<< ` `"\t\t(C) d - b = "` `<< (d - b) ` ` ` `<< ` `"\t\tl = A/k = "` ` ` `<< l << endl; ` ` ` ` ` `cout << ` `"d = "` `<< d ` ` ` `<< ` `"\t\t(D) d + b = "` `<< (d + b) ` ` ` `<< ` `"\t\tm = c/k = "` ` ` `<< m << endl; ` ` ` ` ` `// Printing the factors ` ` ` `if` `(k % 2 == 0 && h % 2 == 0) { ` ` ` `k = k / 2; ` ` ` `h = h / 2; ` ` ` ` ` `cout << ` `"Factors are: "` ` ` `<< ((k) * (k) + (h) * (h)) ` ` ` `<< ` `" "` `<< (l * l + m * m) ` ` ` `<< endl; ` ` ` `} ` ` ` `else` `{ ` ` ` `l = l / 2; ` ` ` `m = m / 2; ` ` ` ` ` `cout << ` `"Factors are: "` ` ` `<< ((l) * (l) + (m) * (m)) ` ` ` `<< ` `" "` `<< (k * k + h * h) ` ` ` `<< endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 100000; ` ` ` ` ` `findFactors(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

a = 316 (A) a - c = 16 k = gcd[A, C] = 8 b = 12 (B) a + c = 616 h = gcd[B, D] = 56 c = 300 (C) d - b = 88 l = A/k = 2 d = 100 (D) d + b = 112 m = c/k = 11 Factors are: 800 125

**Complexity Analysis:**

**Time Complexity:** *O(sqrt(N))*, where N is the given number

**Space Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Fermat's Factorization Method
- Dixon's Factorization Method with implementation
- Fermat's Factorization method for large numbers
- Wheel Factorization Algorithm
- Sum of Factors of a Number using Prime Factorization
- Pollard's Rho Algorithm for Prime Factorization
- Prime Factorization using Sieve O(log n) for multiple queries
- Trial division Algorithm for Prime Factorization
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Transportation Problem | Set 6 (MODI Method - UV Method)
- JQuery | parseXML() Method
- JQuery | trim() Method
- Program for Muller Method
- Program for Bisection Method
- Java Math incrementExact(int x) method
- Horner's Method for Polynomial Evaluation
- Program for Newton Raphson Method
- Primality Test | Set 2 (Fermat Method)
- Babylonian method for square root
- Array.GetValue() Method in C# with Examples | Set - 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.