Dixon’s Factorization method is an integer factorization algorithm. In this article, this method is explained to find the factors of a composite number.

Dixon Factorization is based on the well-known fact of number theory that:

- If with it is likely that
**gcd(x – y, n)**will be factor of**n**.

**For example:**

if N = 84923,

by starting at 292, the first number greater than √N and counting up

5052 mod 84923 = 256 = 16^{2}So (505 – 16)(505 + 16) = 0 mod 84923.

Computing the greatest common divisor of 505 – 16 and N using Euclid’s algorithm gives 163, which is a factor of N.

**Dixon’s Factorization Algorithm:**

**Step 1:**Choose a bound B and identify the factor base (P) of all primes less than or equal to B.**Step 2:**Search for positive integer**z**, such that is B-Smooth.(1)

**B-Smooth**: A positive integer is called B-Smooth if none of its prime factors is greater than B.

**For example:**720 has prime factorization as 2

^{4}* 3^{2}* 5^{1}

Therefore 720 is 5 smooth because none of its prime factors is greater than 5.**Step 3:**After generating enough of these relations (generally few more than the size of P), we use the method of linear algebra (e.g Gaussian Elimination) to multiply together these relations. Repeat this step until we formed a sufficient number of smooth squares.**Step 4:**After multiplying all these relations, we get the final equation say:a

^{2}= b^{2}mod(N)**Step 5:**Therefore, the factors of the above equation can be determined as:GCD(a - b, N), GCD(a + b, N)

**Step by Step execution of the Dixon’s Factorization Algorithm:**

- Let’s say, we want to factor N = 23449 using bound B = 7. Therefore, the factor base P = {2, 3, 5, 7}.
- Here, x = ceil(sqrt(n)) = 154. So, we search randomly for integers between 154 and N whose squares are B-Smooth.
- As mentioned before, a positive integer is called B-Smooth if none of its prime factors is greater than B. So, let’s say, we find two numbers which are 970 and 8621 such that their none of their squares have prime factors greater than 7.
- Starting here the first related squares we get are:
- 970
^{2}% 23499 = 2940 = 2^{2}* 3 * 5 * 7^{2} - 8621
^{2}% 23499 = 11760 = 2^{4}* 3 * 5 * 7^{2}

- 970
- So, (970 * 8621)
^{2}= (2^{3}* 3 * 5 * 7^{2})^{2}% 23499. That is:

14256^{2}= 5880^{2}% 23499. - Now, we find:
gcd(14256 - 5880, 23449) = 131 gcd(14256 + 5880, 23449) = 179

- Therefore, the factors are:

**N = 131 * 179**

Below is the implementation of the above approach:

## C++

`// C++ implementation of Dixon factorization algo ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` `#include<vector> ` ` ` `// Function to find the factors of a number ` `// using the Dixon Factorization Algorithm ` `void` `factor(` `int` `n) ` `{ ` ` ` ` ` `// Factor base for the given number ` ` ` `int` `base[4] = {2, 3, 5, 7}; ` ` ` ` ` `// Starting from the ceil of the root ` ` ` `// of the given number N ` ` ` `int` `start = ` `int` `(` `sqrt` `(n)); ` ` ` ` ` `// Storing the related squares ` ` ` `vector<vector<` `int` `> >pairs; ` ` ` ` ` `// For every number from the square root ` ` ` `// Till N ` ` ` `int` `len=` `sizeof` `(base)/` `sizeof` `(base[0]); ` ` ` `for` `(` `int` `i = start; i < n; i++) ` ` ` `{ ` ` ` `vector<` `int` `> v; ` ` ` ` ` `// Finding the related squares ` ` ` `for` `(` `int` `j = 0; j < len; j++) ` ` ` `{ ` ` ` `int` `lhs = ((` `int` `)` `pow` `(i,2))% n; ` ` ` `int` `rhs = ((` `int` `)` `pow` `(base[j],2)) % n; ` ` ` ` ` `// If the two numbers are the ` ` ` `// related squares, then append ` ` ` `// them to the array ` ` ` `if` `(lhs == rhs) ` ` ` `{ ` ` ` `v.push_back(i); ` ` ` `v.push_back(base[j]); ` ` ` `pairs.push_back(v); ` ` ` `} ` ` ` ` ` `} ` ` ` `} ` ` ` ` ` `vector<` `int` `>newvec; ` ` ` ` ` `// For every pair in the array, compute the ` ` ` `// GCD such that ` ` ` `len = pairs.size(); ` ` ` `for` `(` `int` `i = 0; i < len;i++){ ` ` ` `int` `factor = __gcd(pairs[i][0] - pairs[i][1], n); ` ` ` ` ` `// If we find a factor other than 1, then ` ` ` `// appending it to the final factor array ` ` ` `if` `(factor != 1) ` ` ` `newvec.push_back(factor); ` ` ` ` ` `} ` ` ` `set<` `int` `>s; ` ` ` `for` `(` `int` `i = 0; i < newvec.size(); i++) ` ` ` `s.insert(newvec[i]); ` ` ` `for` `(` `auto` `i = s.begin(); i != s.end(); i++) ` ` ` `cout<<(*i)<<` `" "` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `factor(23449); ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of Dixon factorization algo ` ` ` `from` `math ` `import` `sqrt, gcd ` `import` `numpy as np ` ` ` `# Function to find the factors of a number ` `# using the Dixon Factorization Algorithm ` `def` `factor(n): ` ` ` ` ` `# Factor base for the given number ` ` ` `base ` `=` `[` `2` `, ` `3` `, ` `5` `, ` `7` `] ` ` ` ` ` `# Starting from the ceil of the root ` ` ` `# of the given number N ` ` ` `start ` `=` `int` `(sqrt(n)) ` ` ` ` ` `# Storing the related squares ` ` ` `pairs ` `=` `[] ` ` ` ` ` `# For every number from the square root ` ` ` `# Till N ` ` ` `for` `i ` `in` `range` `(start, n): ` ` ` ` ` `# Finding the related squares ` ` ` `for` `j ` `in` `range` `(` `len` `(base)): ` ` ` `lhs ` `=` `i` `*` `*` `2` `%` `n ` ` ` `rhs ` `=` `base[j]` `*` `*` `2` `%` `n ` ` ` ` ` `# If the two numbers are the ` ` ` `# related squares, then append ` ` ` `# them to the array ` ` ` `if` `(lhs ` `=` `=` `rhs): ` ` ` `pairs.append([i, base[j]]) ` ` ` ` ` `new ` `=` `[] ` ` ` ` ` `# For every pair in the array, compute the ` ` ` `# GCD such that ` ` ` `for` `i ` `in` `range` `(` `len` `(pairs)): ` ` ` `factor ` `=` `gcd(pairs[i][` `0` `] ` `-` `pairs[i][` `1` `], n) ` ` ` ` ` `# If we find a factor other than 1, then ` ` ` `# appending it to the final factor array ` ` ` `if` `(factor !` `=` `1` `): ` ` ` `new.append(factor) ` ` ` ` ` `x ` `=` `np.array(new) ` ` ` ` ` `# Returning the unique factors in the array ` ` ` `return` `(np.unique(x)) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `print` `(factor(` `23449` `)) ` |

*chevron_right*

*filter_none*

**Output:**

[131 179]

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:

- Euler's Factorization method
- Fermat's Factorization Method
- 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
- Implementation of DFS using adjacency matrix
- Implementation of K Nearest Neighbors
- Lychrel Number Implementation
- Fizz Buzz Implementation
- Implementation of BFS using adjacency matrix
- Implementation of Blockchain in Java
- Cubic Bezier Curve Implementation in C
- Implementation of Wilson Primality test
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Travelling Salesman Problem implementation using BackTracking
- Implementation of Non-Preemptive Shortest Job First using Priority Queue