Pollard p-1 Algorithm

01 Mar, 2020
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.
Given a number n.Initialize a = 2, i = 2
Until a factor is returned doa <- (a^i) mod nd <- GCD(a-1, n)if 1 < d < n then    return delse    i <- i+1
Other factor, d' <- n/d
If d' is not prime    n <- d'    goto 1else    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.
# Python code for Pollard p-1 # factorization Method   # importing "math" for # calculating GCDimport math   # importing "sympy" for # checking primeimport sympy         # function to generate # prime factorsdef 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 coden = 1403   # temporarily storing nnum = n   # list for storing prime factorsans = []   # iterated till all prime factors# are obtainedwhile(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 resultprint("Prime factors of", n, "are", *ans)
Output:
Prime factors of 1403 are 61 23
Mathematical
Python
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
Python Dictionary
How to get column names in Pandas dataframe