On this page
Trial division tries 2, 3, 5, 7 and so on until something divides: fine for 8051, painfully slow when the smallest prime factor has ten digits. In 1975 John Pollard published a faster idea in A Monte Carlo method for factorization (BIT, volume 15). It needs only squaring, subtraction and a greatest common divisor, so you can run it by hand.
How does Pollard's rho algorithm work?
Pick a polynomial such as g(x) = x² + 1, work modulo n, and start from x = 2. According to the Wikipedia article on Pollard's rho algorithm, Pollard's original paper used x² − 1; x² + 1 is the more common choice today.
Keep two runners on the same sequence:
the tortoise x, which applies g once per step;
the hare y, which applies g twice per step;
after each step, compute d = gcd(|x − y|, n).
If d = 1, keep going. If 1 < d < n, you have found a factor. If d = n, the run failed: restart with a different starting value or a different constant c in x² + c.
The two-speed trick is Floyd's cycle-finding algorithm, the tortoise and the hare: two pointers move through a sequence at different speeds until they land on equal values, using only a constant amount of memory.
Worked example: how does Pollard's rho factor 8051?
Take n = 8051 and g(x) = x² + 1 mod 8051, starting with x = y = 2. The sequence is 2, 5, 26, 677, 7474, 2839, 871, and so on; the steps below match the table in the Wikipedia example.
Step 1: x = 5, y = 26. |x − y| = 21, and gcd(21, 8051) = 1.
Step 2: x = 26, y = 7474. |x − y| = 7448, and gcd(7448, 8051) = 1.
Step 3: x = 677, y = 871. |x − y| = 194, and gcd(194, 8051) = 97.
Euclid's algorithm finishes the job: 8051 = 41 × 194 + 97, then 194 = 2 × 97 + 0, so the gcd is 97 and 8051 = 83 × 97. New to Euclid's method? Read our Euclid's gcd algorithm, step by step.
Why does Pollard's rho take about √p steps?
The algorithm is really watching a hidden sequence: x mod p, where p is a prime factor you do not know yet. That sequence has only p possible values, so it must repeat. When x ≡ y (mod p) but x ≢ y (mod n), x − y is a multiple of p but not of n, and the gcd pulls p out.
How soon does a repeat come? This is the birthday paradox: the Wikipedia article on the birthday problem notes that only 23 people are needed for the chance of a shared birthday to pass 50%, far fewer than the 365 possible days. If x² + 1 behaves like a random sequence, a repeat among p values is expected after roughly √p steps. That is why the expected running time is proportional to the square root of the smallest prime factor, not to n itself, though the article notes this is a heuristic without a rigorous proof.
For 8051, √97 is a little under 10, and the mod-97 sequence repeated after four values. The method is fast for small factors but slower when all the factors are large.
Why is it called the rho algorithm?
Draw the values of x mod p as dots with an arrow from each to the next. Because each value depends only on the one before it, the path runs along a tail and then closes into a loop: the Greek letter ρ. In the diagram above, mod 97 has a tail of one value feeding a loop of three; mod 83 has a tail of four feeding a loop of five.
The hare catches the tortoise at the first step that is at least the tail length and a multiple of the loop length: step 3 mod 97, step 5 mod 83. The earlier meeting wins, so 97 appears first.
How can you practise Pollard's rho by hand?
Try 10403, the second worked example in the Wikipedia article (101 × 103), before you write any code:
Make three columns: x, y and gcd(|x − y|, n).
Each row, apply g once to x and twice to y, reducing mod n.
Find the gcd with Euclid's algorithm, not by factoring |x − y|.
Stop at the first gcd strictly between 1 and n.
Reduce your x column mod each factor and draw both ρ shapes.
Rho finds factors; it does not test primality, which has its own traps, as our post on why Carmichael numbers like 561 fool Fermat's test shows. If modular arithmetic or the gcd still feels shaky, the Fermi Concept Library covers every concept in mathematics and computing. For worked problems, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



