Pollard p-1 Algorithm
Last Updated :
09 Sep, 2022
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.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n == 2 || n == 3)
return true ;
if (n % 2 == 0)
return false ;
for ( int i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false ;
return true ;
}
int pollard( int n)
{
long long a = 2;
int i = 2;
while ( true )
{
a = (( long long ) pow (a, i)) % n;
a += n;
a %= n;
int d = gcd(a-1,n);
if (d > 1)
{
return d;
break ;
}
i += 1;
}
}
int main()
{
int n = 1403;
int num = n;
vector< int > ans;
while ( true )
{
int d = pollard(num);
ans.push_back(d);
int r = (num/d);
if (isPrime(r))
{
ans.push_back(r);
break ;
}
else
num = r;
}
cout << "Prime factors of " << n << " are " ;
for ( int elem : ans)
cout << elem << " " ;
}
|
Java
import java.util.*;
class GFG
{
static long gcd( long a, long b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static boolean isPrime( long n)
{
if (n <= 1 )
return false ;
if (n == 2 || n == 3 )
return true ;
if (n % 2 == 0 )
return false ;
for ( long i = 3 ; i * i <= n; i += 2 )
if (n % i == 0 )
return false ;
return true ;
}
static long pollard( long n)
{
long a = 2 ;
long i = 2 ;
while ( true )
{
a = (( long ) Math.pow(a, i)) % n;
a += n;
a %= n;
long d = gcd(a- 1 ,n);
if (d > 1 )
{
return d;
}
i += 1 ;
}
}
public static void main(String[] args)
{
long n = 1403 ;
long num = n;
ArrayList<Long> ans = new ArrayList<Long>();
while ( true )
{
long d = pollard(num);
ans.add(d);
long r = (num/d);
if (isPrime(r))
{
ans.add(r);
break ;
}
else
num = r;
}
System.out.print( "Prime factors of " + n + " are " );
for ( long elem : ans)
System.out.print(elem + " " );
}
}
|
Python3
import math
import sympy
def pollard(n):
a = 2
i = 2
while ( True ):
a = (a * * i) % n
d = math.gcd((a - 1 ), n)
if (d > 1 ):
return d
break
i + = 1
n = 1403
num = n
ans = []
while ( True ):
d = pollard(num)
ans.append(d)
r = int (num / d)
if (sympy.isprime(r)):
ans.append(r)
break
else :
num = r
print ( "Prime factors of" , n, "are" , * ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static long gcd( long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static bool isPrime( long n)
{
if (n <= 1)
return false ;
if (n == 2 || n == 3)
return true ;
if (n % 2 == 0)
return false ;
for ( long i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false ;
return true ;
}
static long pollard( long n)
{
long a = 2;
long i = 2;
while ( true )
{
a = (( long ) Math.Pow(a, i)) % n;
a += n;
a %= n;
long d = gcd(a-1,n);
if (d > 1)
{
return d;
}
i += 1;
}
}
public static void Main( string [] args)
{
long n = 1403;
long num = n;
List< long > ans = new List< long >();
while ( true )
{
long d = pollard(num);
ans.Add(d);
long r = (num/d);
if (isPrime(r))
{
ans.Add(r);
break ;
}
else
num = r;
}
Console.Write( "Prime factors of " + n + " are " );
foreach ( long elem in ans)
Console.Write(elem + " " );
}
}
|
Javascript
function gcd(x, y)
{
x = Math.abs(x);
y = Math.abs(y);
while (y) {
var t = y;
y = x % y;
x = t;
}
return x;
}
function isPrime(n)
{
if (n <= 1)
return false ;
if (n == 2 || n == 3)
return true ;
if (n % 2 == 0)
return true ;
for ( var i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false ;
return true ;
}
function pollard(n)
{
let a = 2
let i = 2
while ( true )
{
a = (a**i) % n
d = gcd((a-1), n)
if (d > 1)
{
return d
break
}
i += 1
}
}
let n = 1403
let num = n
let ans = []
while ( true )
{
let d = pollard(num)
ans.push(d)
r = Math.floor(num/d)
if (isPrime(r))
{
ans.push(r)
break
}
else
num = r
}
console.log( "Prime factors of" , n, "are" , ans.join( " " ))
|
Output:
Prime factors of 1403 are 61 23
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...