Short answer: Pollard’s rho looks for a nontrivial factor of a composite n by iterating a polynomial, usually x² + c, modulo n, and taking gcd of n with the difference of two values on the sequence. By the birthday paradox the sequence cycles quickly modulo a secret prime factor p, and the gcd sees p when two iterates are congruent modulo p and not modulo n. With Floyd’s cycle finding the expected time is about O(n^{1/4}) arithmetic operations. The method is Monte Carlo and can fail for an unlucky c.
What the problem actually asks
Trial division up to sqrt(n) is exact and hopeless when the smallest prime factor is large. A random map on the residues modulo p collides in about sqrt(p) steps. The algorithm cannot see the residues modulo p, because p is the unknown. It iterates modulo n and asks the gcd to reveal the collision. That is the whole trick.
The name rho is the shape of the sequence: a tail into a cycle. Floyd’s tortoise and hare find the cycle without storing every iterate. Brent’s cycle algorithm is a common replacement with fewer evaluations.
What the algorithm guarantees
If the gcd returns d with 1 < d < n, then d is a nontrivial factor. If it returns n, that c and start point failed and another c is tried. If it returns 1, the walk continues. The expected cost to find the smallest prime factor p is O(n^{1/4}) up to logarithmic factors from arithmetic, because sqrt(p) ≤ n^{1/4}. Extra memory is a few integers. The algorithm does not always terminate on a product of two close large primes within a short budget. It is a practical factoriser for moderate factors, not a proof of the difficulty of RSA moduli.
A prime input never yields a nontrivial factor. Miller-Rabin should screen the input. The polynomial x² + c must not be so structured that the map is not mixing. c = 0 and c = −2 are the classical bad choices.
The working steps
- If n is even, return 2. If a primality test says n is prime, stop. There is no nontrivial factor.
- Choose c, often 1, and a start x. Set the tortoise and the hare to x.
- Repeat: move the tortoise one step and the hare two steps under the map z → z² + c modulo n. Compute g = gcd(|hare − tortoise|, n).
- If g is 1, continue. If g is n, restart with a different c or start. If g is anything else, return g.
- Optionally use Brent’s cycle detection in place of Floyd’s to reduce the number of polynomial evaluations. The gcd test is the same.
The n^{1/4} figure is an expectation for the smallest factor, not a worst-case theorem and not a factorisation of a balanced RSA modulus in reasonable time. The gcd can return n, which is a failed run, not a factor. Deterministic factorisation of a small integer can just use trial division or the linear sieve. Rho is for one integer whose small factor is unknown.
Approaches that look simpler and fail
| Approach | Why it fails when a gcd of n is treated as a factor |
|---|---|
| Return n when the gcd equals n | Every difference was divisible by n. The run found the trivial factor. Change c and restart. |
| Store every iterate to find the cycle | The result can be correct and the memory is the length of the rho, which is what Floyd’s two pointers avoid. |
| Use c = 0 | The map x² modulo n is a poor pseudorandom walk for this method. Choose a small nonzero c other than the known degenerate cases. |
| Quote the running time as O(n^{1/4}) worst case | The birthday argument is expected time for a random map. An unlucky polynomial or a prime input does not obey a deterministic clock. |
Primality, before any attempt to factor, is Miller-Rabin. Bulk factorisation of every integer up to n is the linear sieve. Pollard’s rho is the birthday-paradox factoriser for one composite.
A working example
This Python fragment walks x and x²+1 modulo n with a tortoise and a hare, and takes a gcd along the way. For 91 the gcd is a nontrivial factor, 7.
import math
n, c = 91, 1
tortoise = hare = 2
factor = 1
while factor == 1:
tortoise = (tortoise * tortoise + c) % n
hare = (hare * hare + c) % n
hare = (hare * hare + c) % n
factor = math.gcd(abs(hare - tortoise), n)
assert factor not in (1, n) and n % factor == 0
print(factor)
Where it is used
Pollard’s rho finds a small prime factor of a composite, and it is one of the methods inside GMP’s factoring. A balanced RSA modulus is not attacked by this walk; the method is for integers that have a small factor. The elliptic-curve method is the other factoring tool in that toolbox.
When you can drop this algorithm
Drop rho when n is prime or when trial division up to a small bound already found the factor. Keep it to peel a moderate prime factor from a large composite, and restart when the gcd comes back equal to n.
Why this stays published
We publish this so a search for Pollard’s rho returns the polynomial walk, the gcd with the tortoise-hare difference, and the expected n to the one-fourth cost. Pollard’s rho finds a nontrivial factor by walking x² + c modulo n and taking a gcd when Floyd’s cycle pointers meet, in expected time about n to the one-fourth.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
