← LibraryTrial Division up to a Small BoundEngineering · MathematicsLesson 118/385← PrevNext →
ArticlePublished 7 Aug 20263 min readBy Kevin Jogin

Engineering  /  Mathematics  — Primality Testing

Trial Division up to a Small Bound

Choosing the trial division bound ahead of a probabilistic test, and the cost balance that determines it.

Page KV-MATH-0390Reading time 3 minReviewed 2026-08-07Author Kevin Jogin

Executive summary

Trial division ahead of Miller-Rabin removes most composites cheaply. How far to divide is an optimisation with a clear structure: division cost rises linearly in the bound while the rejection rate improves only logarithmically.

Mertens' theorem supplies the survival rate, making the optimum computable rather than guessed.

Learning objectives

  1. Quantify the survival rate as a function of the bound.
  2. Balance division cost against saved exponentiations.
  3. Implement the filter efficiently.

01Survival rate

The proportion of odd integers with no prime factor below y follows from Mertens' theorem.

survival ≈ 2 e^{−γ} / ln y    (the factor 2 because even numbers are excluded already)
Filter survival by bound
Bound yPrimes below yOdd candidates surviving
10025≈ 24%
1,000168≈ 16%
10,0001,229≈ 12%
65,5366,542≈ 10%

02The cost balance

  1. Cost of filtering

    Roughly π(y) single-precision divisions per candidate, or one multiprecision gcd.

  2. Cost of a Miller-Rabin round

    One modular exponentiation, O(len(n)³) — thousands of times more expensive.

  3. Expected total

    candidates × filter cost + survivors × exponentiation cost.

  4. Optimise

    Increase y while the marginal reduction in survivors saves more than the added divisions cost.

Because an exponentiation costs so much more than a division, the optimum sits well beyond where the survival curve has flattened. Bounds in the low thousands to tens of thousands are typical, and the optimum is broad — anywhere in that range performs within a few per cent of the best.

For larger moduli the balance shifts upward, since the exponentiation cost grows cubically in the bit length while the division cost is roughly constant.

03Efficient implementation

Rather than dividing by each small prime in turn, compute a single gcd against a precomputed product.

Algorithm

Filter by gcd against a primorial

Inputcandidate n, precomputed primorial P
Outputreject, or pass to Miller-Rabin
  1. Precompute P = product of all primes in (2, y].
  2. For each candidate n:
  3.   Compute g = gcd(n, P).
  4.   If g ≠ 1, reject n as composite.
  5.   Otherwise pass n to the probabilistic test.
Cost  one gcd, O(len(n) · len(P))

A further refinement used in incremental search: compute the residue of a base candidate against each small prime once, then update the residues by addition as the candidate is stepped, avoiding a fresh division per step. This is the standard sieve-based generation technique.

04Frequently asked questions

Why exclude 2 from the primorial?

Because candidates are already odd by construction. Including 2 would make every gcd even and reject everything.

Is one large gcd really faster than many small divisions?

For multiprecision candidates, yes, because the divisions are all multiprecision-by-single-precision and there are thousands of them. The gcd is a single operation on operands of comparable size.

Does the optimum depend on the Miller-Rabin round count?

Only weakly. Most rejected candidates fail on the first round, so the marginal cost of a survivor is roughly one exponentiation regardless of how many rounds a survivor would eventually receive.

Sources and method

Structural reference: Victor Shoup, A Computational Introduction to Number Theory and Algebra, Version 1, Cambridge University Press, 2005 — book pages 255-258.

This page carries the durable method layer only: definitions, constructions, algorithms, complexity results and selection criteria, authored originally for KEVOS. No text is transcribed or paraphrased from the source, and no numeric tables or benchmark data are reproduced — these are routed to live authoritative sources instead.

Author: Kevin Jogin. Last reviewed 2026-08-07.

Continue learning

Generating a Random Prime Between 2 and MArticle · MathematicsNEXT LESSON →Generating a Random k-Bit Prime with Miller-RabinArticle · MathematicsThe Miller-Rabin Primality TestArticle · MathematicsPerfect Power Testing and Prime Power FactoringArticle · Mathematics