Pollard p-1 Algorithm

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 : 1403
Output : Prime factors of 1403 are 61 23.
Explanation : n = 1403, a = 2, i = 2

1st Iteration:
    a = (2^2) mod 1403 = 4
    d = GCD(3, 1403) = 1
    i = 2 + 1 = 3

2nd Iteration:
    a = (4^3) mod 1403 = 64
    d = GCD(63, 1403) = 1
    i = 3 + 1 = 4

3rd Iteration:
    a = (64^4) mod 1403 = 142
    d = GCD(141, 1403) = 1
    i = 4 + 1 = 5

4th 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 : 2993
Output : Prime factors of 2993 are 41 73.

Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.