# Definition(s)

Pollard's rho algorithm is an algorithm for integer factorization. It was invented by John Pollard in 1975. It uses only a small amount of space, and its expected running time is proportional to the square root of the size of the smallest prime factor of the composite number being factorized.

For an awesome tutorial check this.

Pollard's Rho Algorithm Scheme

``````a, b = 2, 2;

while ( b != a ){
a = f(a);     // a runs once
b = f(f(b));  // b runs twice as fast
p = GCD(| b - a |, N);

if ( p > 1)
return "Factor: p";
}

return "Failure"``````

# Algorithm(s)

``````

In :

from math import gcd
from sympy.ntheory import isprime  # primality test
import random

``````
``````

In :

def pollards_rho(n, seed=2, f=lambda x: x ** 2 + 1):
if n % 2 == 0:
return 2

a, b, d = seed, seed, 1
while d == 1:
a = f(a) % n
b = f(f(b) % n) % n
d = gcd(a - b, n)

return None if d == n else d

``````
``````

In :

def impl_factor(n):
if n < 2:
return []

if isprime(n):
return [n]

f = pollards_rho(n)
while f is None:
a = random.randrange(2, 10 ** 6)
b = random.randrange(2, 10 ** 6)
f = pollards_rho(n, seed=b, f=lambda x: x ** 2 + a)

tmp, factors = impl_factor(f), []
while n % f == 0:
factors += tmp
n = n // f

return factors + impl_factor(n)

def factor(n):
fact = impl_factor(n)
fact.sort()
return fact

``````

# Run(s)

``````

In :

factor(100)

``````
``````

Out:

[2, 2, 5, 5]

``````
``````

In :

factor(32131245432423)

``````
``````

Out:

[3, 773, 1973, 7022629]

``````
``````

In :

factor(2 ** 5 * 3 ** 10 * 5 * 7 ** 2)

``````
``````

Out:

[2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 7, 7]

``````
``````

In :

factor(213122112213321312321312)

``````
``````

Out:

[2, 2, 2, 2, 2, 3, 3, 7187077, 102963601763837]

``````
``````

In :

factor(567843720532049232390851243214723895239171032)

``````
``````

Out:

[2, 2, 2, 11743, 1430357, 4225862227183602989314916784386129]

``````