Factorizing a large odd integer, **n**, into its corresponding prime factors can prove to be a difficult task. A brute approach can be testing all integers less than n until a divisor is found. This proves to be very time consuming as a divisor might be a very large prime itself.

**Pollard p-1 algorithm** is a better approach to find out prime factors of any integer. Using the combined help of Modular Exponentiation and GCD, it is able to calculate all the distinct prime factors in no time.

#### Algorithm

- Given a number n.

Initialize a = 2, i = 2 - Until a factor is returned do

a <- (a^i) mod n

d <- GCD(a-1, n)

if 1 < d < n then

return d

else

i <- i+1 - Other factor, d' <- n/d
- If d' is not prime

n <- d'

goto 1

else

d and d' are two prime factors.

In the above algorithm, the power of ‘a’ is continuously raised until a factor, ‘d’, of n is obtained. Once d is obtained, another factor, ‘d”, is n/d. If d’ is not prime, the same task is repeated for d’

**Examples:**

Input :1403Output :Prime factors of 1403 are 61 23.Explanation :n = 1403, a = 2, i = 21st Iteration:a = (2^2) mod 1403 = 4 d = GCD(3, 1403) = 1 i = 2 + 1 = 32nd Iteration:a = (4^3) mod 1403 = 64 d = GCD(63, 1403) = 1 i = 3 + 1 = 43rd Iteration:a = (64^4) mod 1403 = 142 d = GCD(141, 1403) = 1 i = 4 + 1 = 54th Iteration:a = (142^5) mod 1403 = 794 d = GCD(793, 1403) = 61 Since 1 < d < n, one factor is 61. d' = 1403 / 61 = 23.Input :2993Output :Prime factors of 2993 are 41 73.

Below is the implementation.

`# Python code for Pollard p-1 ` `# factorization Method ` ` ` `# importing "math" for ` `# calculating GCD ` `import` `math ` ` ` `# importing "sympy" for ` `# checking prime ` `import` `sympy ` ` ` ` ` `# function to generate ` `# prime factors ` `def` `pollard(n): ` ` ` ` ` `# defining base ` ` ` `a ` `=` `2` ` ` ` ` `# defining exponent ` ` ` `i ` `=` `2` ` ` ` ` `# iterate till a prime factor is obtained ` ` ` `while` `(` `True` `): ` ` ` ` ` `# recomputing a as required ` ` ` `a ` `=` `(a` `*` `*` `i) ` `%` `n ` ` ` ` ` `# finding gcd of a-1 and n ` ` ` `# using math function ` ` ` `d ` `=` `math.gcd((a` `-` `1` `), n) ` ` ` ` ` `# check if factor obtained ` ` ` `if` `(d > ` `1` `): ` ` ` ` ` `#return the factor ` ` ` `return` `d ` ` ` ` ` `break` ` ` ` ` `# else increase exponent by one ` ` ` `# for next round ` ` ` `i ` `+` `=` `1` ` ` `# Driver code ` `n ` `=` `1403` ` ` `# temporarily storing n ` `num ` `=` `n ` ` ` `# list for storing prime factors ` `ans ` `=` `[] ` ` ` `# iterated till all prime factors ` `# are obtained ` `while` `(` `True` `): ` ` ` ` ` `# function call ` ` ` `d ` `=` `pollard(num) ` ` ` ` ` `# add obtained factor to list ` ` ` `ans.append(d) ` ` ` ` ` `# reduce n ` ` ` `r ` `=` `int` `(num` `/` `d) ` ` ` ` ` `# check for prime using sympy ` ` ` `if` `(sympy.isprime(r)): ` ` ` ` ` `# both prime factors obtained ` ` ` `ans.append(r) ` ` ` ` ` `break` ` ` ` ` `# reduced n is not prime, so repeat ` ` ` `else` `: ` ` ` ` ` `num ` `=` `r ` ` ` `# print the result ` `print` `(` `"Prime factors of"` `, n, ` `"are"` `, ` `*` `ans) ` |

*chevron_right*

*filter_none*

**Output:**

Prime factors of 1403 are 61 23

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:

- Bisect Algorithm Functions in Python
- Page Rank Algorithm and Implementation
- Simplex Algorithm - Tabular Method
- Asynchronous Advantage Actor Critic (A3C) algorithm
- Cristian's Algorithm
- Python | Foreground Extraction in an Image using Grabcut Algorithm
- k-nearest neighbor algorithm in Python
- ML | T-distributed Stochastic Neighbor Embedding (t-SNE) Algorithm
- ML | Mini Batch K-means clustering algorithm
- ML | Reinforcement Learning Algorithm : Python Implementation using Q-learning
- Silhouette Algorithm to determine the optimal value of k
- Implementing DBSCAN algorithm using Sklearn
- ML | ECLAT Algorithm
- Implementing Apriori algorithm in Python
- Explanation of Fundamental Functions involved in A3C algorithm
- ML | Handling Imbalanced Data with SMOTE and Near Miss Algorithm in Python
- ML | Frequent Pattern Growth Algorithm
- Epsilon-Greedy Algorithm in Reinforcement Learning
- Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm
- Python | Single Point Crossover in Genetic Algorithm

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.