Lab · Algorithms
Pollard's rho
Square and add one, over and over, modulo the number you want to factor. Modulo a hidden prime factor the sequence soon runs into a loop shaped like the Greek letter ρ, and a single gcd gives the factor away.
To factor $n$, iterate $x \mapsto x^2 + c \pmod n$ from $x_0 = 2$. Suppose $p$ is an unknown prime factor of $n$. Reduced modulo $p$, the sequence lives in a set of only $p$ values, so it must eventually repeat, and from then on it cycles: a tail followed by a loop, the shape of a $\rho$. A repeat modulo $p$ means $p \mid x_i - x_j$, so $\gcd(x_i - x_j, n)$ is a nontrivial factor of $n$, unless by bad luck the sequence repeats modulo every factor at once.
John Pollard published the method in 1975. It needs almost no memory: Floyd's tortoise and hare compares $x_i$ with $x_{2i}$, and once both are on the loop the hare gains one step per round on the tortoise, so they meet within one loop length.
Why it takes about √p steps
If $x \mapsto x^2 + c$ behaved like a random map on $\mathbb{Z}/p\mathbb{Z}$, the first repeat would come after about $\sqrt{\pi p/2}$ steps, by the same calculation as the birthday paradox (23 people suffice for a shared birthday because $23 \approx \sqrt{2 \cdot 365 \ln 2}$). Nobody has proved that $x^2 + c$ is random enough, but in practice it is, so Pollard's rho finds a factor $p$ in about $\sqrt p$ multiplications, compared with $p$ trial divisions. For $n = 600851475143$, the factor $71$ appears after just eleven steps.
The method made history: Brent and Pollard used a refinement of it in 1980 to find the factor $1238926361552897$ of the Fermat number $F_8 = 2^{256} + 1$. Today it is the standard way to strip small factors before heavier algorithms (the elliptic curve method, the quadratic and number field sieves) take over, and the same tortoise-and-hare idea solves discrete logarithms on elliptic curves in about the square root of the group size, which is what sets the key sizes in the elliptic curve lab.
Full proof: the tortoise and the hare must meet
Lemma. Let $y_0, y_1, \dots$ be a sequence in a finite set with $y_{k+1} = g(y_k)$, with tail length $\mu$ and cycle length $\lambda$ (so $y_i = y_j$ for $i, j \ge \mu$ exactly when $\lambda \mid i - j$). Then there is $i$ with $1 \le i \le \mu + \lambda$ and $y_i = y_{2i}$.
Proof. For $i \ge \mu$, $y_i = y_{2i}$ holds exactly when $\lambda \mid 2i - i = i$. Among the $\lambda$ consecutive integers $\mu, \mu + 1, \dots, \mu + \lambda - 1$ exactly one is a multiple of $\lambda$; call it $i$ (take $i = \lambda$ if $\mu = 0$). Then $1 \le i < \mu + \lambda$ and $y_i = y_{2i}$. $\square$
Applying this to $y_k = x_k \bmod p$ shows that $p$ divides $x_i - x_{2i}$ within $\mu + \lambda$ steps, so the gcd becomes divisible by $p$. It equals $n$ only if the same $i$ works for every prime factor of $n$ simultaneously, in which case one restarts with a different $c$.
Further reading
- J. M. Pollard, A Monte Carlo method for factorization, BIT Numerical Mathematics 15 (1975).
- R. P. Brent and J. M. Pollard, Factorization of the eighth Fermat number, Mathematics of Computation 36 (1981).
- R. Crandall and C. Pomerance, Prime Numbers: A Computational Perspective, 2nd ed., Springer, 2005, Chapter 5.