galois.pollard_rho

galois.pollard_rho(n: int, c: int = 1) int

Attempts to find a non-trivial factor of n using cycle detection.

Parameters
n: int

An odd composite integer n>2 that is not a prime power.

c: int = 1

The constant offset in the function f(x)=x2+c mod n. The default is 1. A requirement of the algorithm is that c{0,2}.

Returns

A non-trivial factor m of n, if found. None if not found.

Raises

RuntimeError – If a non-trivial factor cannot be found.

See also

factors, pollard_p1

Notes

Pollard’s ρ algorithm seeks to find a non-trivial factor of n by finding a cycle in a sequence of integers x0,x1, defined by xi=f(xi1)=xi12+1 mod p where p is an unknown small prime factor of n. This happens when xmx2m (mod p). Because p is unknown, this is accomplished by computing the sequence modulo n and looking for gcd(xmx2m,n)>1.

References

Examples

Pollard’s ρ is especially good at finding small factors.

In [1]: n = 503**7 * 10007 * 1000003

In [2]: galois.pollard_rho(n)
Out[2]: 503

It is also efficient for finding relatively small factors.

In [3]: n = 1182640843 * 1716279751

In [4]: galois.pollard_rho(n)
Out[4]: 1716279751

Last update: Apr 21, 2022