← LibraryPollard's Rho Factoring MethodEngineering · MathematicsLesson 2/8← PrevNext →
GuidePublished 6 Aug 20264 min readBy Kevin JoginComputational Number TheoryFactoringPollard RhoCycle Detection
Skip to the main content

MathematicsFactoring

Pollard's Rho Factoring Method

A birthday-paradox collision search in a set you cannot see — constant memory, and expected cost proportional to the fourth root of n.

Executive summary

Find a collision modulo an unknown prime

Iterate a pseudorandom map modulo n. The sequence, viewed modulo an unknown prime factor p, must eventually cycle — and by the birthday bound it does so after about √p steps, far sooner than the cycle modulo n. A collision modulo p but not modulo n makes the difference of two terms divisible by p and not by n, so a GCD reveals the factor.

Learning objectives

  • Explain the birthday argument giving the √p expected running time.
  • Compare Floyd and Brent cycle detection.
  • Implement GCD batching and handle its failure mode.
  • Recognise when rho is the right tool and when it is not.
  • Describe the adaptation to discrete logarithms.

Section 01The idea

Iterate x ↦ x2 + c modulo n. Modulo an unknown prime p dividing n, the sequence takes values in a set of size p and must repeat. By the birthday bound, a repeat occurs after about √p steps — and since p ≤ √n, that is about n1/4 steps.

gcd(xixj, n) > 1   when xixj (mod p)

The shape of the sequence — a tail leading into a cycle — is what gives the method its name.

The unseen collision

The collision happens modulo p, which is unknown, and is detected only through the GCD. The algorithm never observes the residues it is actually exploiting — an unusually elegant piece of indirection.

Section 02Cycle detection

AlgorithmPollard's rho with Brent cycle detectionin: composite n  →  out: a non-trivial factor, or failure
  1. Choose a random c ≠ 0, −2 and set x ← y ← 2, r ← 1.
  2. Set y ← x. For j = 1, …, r: set x ← x2 + c mod n. Advance x by r steps from the saved y.
  3. Set k ← 0. While k < r:
  4.    Accumulate q ← q · (y − x) mod n over a batch of steps, advancing x each time.
  5.    Compute g ← gcd(q, n). If 1 < g < n, return g.
  6.    If g = n, back up and test the batch one step at a time.
  7. Double r and return to step 2. If the search exceeds its budget, restart with a new c.
Brent's variant uses about 25% fewer function evaluations than Floyd's tortoise-and-hare and requires no backtracking of the slow pointer.
Cycle detection compared
MethodEvaluations per stepMemoryNotes
Floyd3 (one slow, two fast)O(1)Simple; the textbook version
Brent1 plus periodic savesO(1)Faster in practice; the usual choice
Distinguished points1O(1) per stored pointUsed in parallel and distributed variants

Section 03GCD batching

A GCD costs far more than a modular multiplication, so differences are accumulated into a product and a single GCD is taken per batch of 100 or so steps.

Batching can lose the factor

If the batch product becomes divisible by all of n — because two different primes collided within the same batch — the GCD returns n and the factor is lost. The remedy is to back up and replay the batch step by step. Implementations that omit this fallback fail on a small but real fraction of inputs.

O(n1/4)expected running time
O(1)memory
≈ 20 digitspractical factor size limit

Section 04Scope and variants

  • What size is the expected factor?
    • Under about 20 digits Pollard ρ — minimal setup, constant memory, ideal after trial division.
    • 20 to 60 digits ECM — cost depends on the factor size, not on n.
    • Balanced factors of a large n Quadratic sieve or NFS — cost depends on n.
Variant

Brent's improvement

Better cycle detection and batched GCDs; the standard implementation.

Variant

Rho for discrete logarithms

The same collision idea in a group, with the iteration tracking exponents so that a collision yields a linear relation and hence the logarithm.

Variant

Parallel rho

Distinguished points allow many machines to search independently with a linear speedup — the basis of distributed discrete logarithm records.

Perfect powers and small n

Rho degenerates on perfect powers, where the sequence modulo p and modulo n cycle together. Test for perfect powers first, and use trial division for very small n where the setup cost dominates.

ReferenceFrequently asked questions

Why avoid c = 0 and c = -2?

Because those choices make the iteration degenerate: the map has special structure that shortens the cycle drastically and destroys the birthday behaviour the analysis assumes. Any other small c works, and restarting with a new c is the standard response to failure.

Is the running time guaranteed?

No — it is an expected time based on a heuristic that the iteration behaves like a random map. The heuristic matches observation closely, but there is no proof, and individual runs can take considerably longer.

Why does rho fail on some inputs?

Either the budget is exhausted before a collision occurs, or a batched GCD returns n. Both are handled by restarting with a different c or by replaying a batch step by step, so failure is recoverable rather than terminal.

NavigateContinue in this stream

Curated next steps from this page. The site also surfaces algorithmically related reading below.

ProvenanceSources and further reading

This page is an original KEVOS explanatory article. It presents the underlying mathematics — definitions, algorithms, complexity results and selection criteria — in KEVOS editorial voice. No text is reproduced from any copyrighted source. Where numerical tables are relevant, KEVOS links to live authoritative databases rather than republishing static values.

Page ID
KV-MATH-0050
Taxonomy
ENG-MATH — Engineering / Mathematics
Collection
COL-CANT-001
Topic stream
CANT-FACTORING
Version
1.1.0 / content 2026.08
Last reviewed
2026-08-06

Continue learning

Classical Factoring: Trial Division, Fermat and LehmanGuide · MathematicsNEXT LESSON →Shanks's SQUFOF Factoring MethodGuide · MathematicsPollard's p−1 Method and Its RelativesGuide · MathematicsThe Continued Fraction Factoring MethodGuide · Mathematics