← LibraryThe Distribution of PrimesEngineering · MathematicsLesson 12/32← PrevNext →
ArticlePublished 6 Aug 2026Updated 5 Aug 20269 min readBy Kevin Jogin
KEVOS® Knowledge Library · Engineering → Mathematics

Engineering/Mathematics/Analytic number theory

The Distribution of Primes

Key generation is a sampling problem: draw random integers of a given size until one is prime. Whether that loop terminates quickly is a question about prime density, and the answer — roughly one in ln n — is the practical content of a century of analytic number theory.

  • Density theory
  • Number theory
  • Underwrites key generation
  • ≈16 min read
  • Analytic results
π(x) ~ x/ln xPrime number theoremThe number of primes up to x, asymptotically. Proved in 1896 by Hadamard and de la Vallée Poussin.
1 in 7102048-bit oddsAmong odd 2048-bit integers, roughly one in 710 is prime — so a few hundred candidates suffice per key.
Θ(x/ln x)ChebyshevCorrect order of magnitude, provable by elementary means from binomial coefficient estimates.
O(n log log n)Sieve costTime to list every prime below n with the sieve of Eratosthenes.

01

Executive summary

Primes thin out, but slowly and predictably. The prime counting function π(x) satisfies π(x) ~ x/ln x, so the density of primes near x is about 1/ln x. For a k-bit number the density is about 1/(k ln 2), and restricting to odd candidates doubles it.

Three tiers of result matter to a practitioner. Chebyshev's elementary bounds fix the order of magnitude and are enough to prove that key generation terminates. Mertens' theorems control sums over primes and appear in sieve analysis. The prime number theorem and its arithmetic-progression refinement give the sharp constants used to estimate work per key and to justify the use of structured primes.

ΘChebyshev, 1852Elementary bounds: c₁x/ln x ≤ π(x) ≤ c₂x/ln x.
~PNT, 1896The asymptotic equality, via complex analysis and the zeros of ζ.
li(x)Better estimateπ(x) ≈ ∫₂ˣ dt/ln t is far more accurate than x/ln x.
π(x)/φ(d)Dirichlet + PNTPrimes are asymptotically evenly split among the φ(d) admissible residues mod d.
Contents

02

The prime counting function

Definition D1

π, θ and ψ

π(x) counts primes up to x. Analytic work prefers the weighted variants θ(x) = ∑p ≤ x log p and ψ(x) = ∑pk ≤ x log p, which are equivalent to π for asymptotic purposes: each of π(x) ~ x/ln x, θ(x) ~ x and ψ(x) ~ x implies the others.

How many primes there are
xπ(x)x/ln xli(x)Relative error of x/ln x
103168145178≈ 14%
10678,49872,38278,628≈ 7.8%
10950,847,53448,254,94250,849,235≈ 5.1%
101237,607,912,01836,191,206,82537,607,950,281≈ 3.8%
101824,739,954,287,740,860≈ 2.4128×1016≈ 2.4740×1016≈ 2.5%

The convergence of x/ln x is genuinely slow; li(x) tracks π(x) far more closely, and the difference between them is the subject of the Riemann hypothesis.

The engineering form of the result

Near x, the chance that a random integer is prime is about 1/ln x. For a k-bit modulus factor, ln x ≈ 0.693k. At k = 1024 that is about 1/710 for odd candidates, and pre-sieving by small primes below 1000 removes roughly 80% of candidates before any expensive test runs.

Contents

03

Chebyshev's theorem and Bertrand's postulate

Theorem T1

Chebyshev bounds

There are absolute constants 0 < c1 < c2 with c1x/ln x ≤ π(x) ≤ c2x/ln x for all x ≥ 2. The proof is elementary: the central binomial coefficient C(2n,n) is divisible by every prime in (n, 2n] and is bounded above by 4n, which pins θ(x) between linear bounds.

Theorem T2

Bertrand's postulate

For every integer n ≥ 1 there is a prime p with n < p ≤ 2n. Proved by Chebyshev in 1852; Erdős's 1932 proof by careful analysis of the prime factorization of C(2n,n) is the version usually taught.

Why the elementary results are enough for algorithms

A randomised prime generator needs only a lower bound of the form c·x/ln x to prove that the expected number of trials is O(ln x). Chebyshev's bound supplies that with no analytic machinery, so the correctness and expected running time of key generation do not depend on the prime number theorem — only the sharp constants do.

Bertrand's postulate plays the analogous role for existence questions: it guarantees a prime in every dyadic interval, so “find a prime with exactly k bits” is always satisfiable, and it supplies the padding argument in several combinatorial proofs.

Contents

04

Mertens' theorems

Sums and products over primes appear whenever a sieve is analysed. Mertens' results give them precisely:

p ≤ x (log p)/p = log x + O(1)  ·  p ≤ x 1/p = log log x + A + O(1/log x)A ≈ 0.2615 is the Meissel–Mertens constant
p ≤ x (1 − 1/p) = e−γ/log x · (1 + o(1))γ ≈ 0.5772 is Euler's constant; the factor e^{−γ} ≈ 0.5615 is the reason naive sieve heuristics are off by about a factor of two
What each statement controls
ResultUsed for
∑ 1/p divergesProves there are infinitely many primes, and that the divergence is only log-logarithmic — primes are sparse but not summably so
∏(1−1/p) asymptoticDensity of integers with no small prime factor; the survival rate of trial-division pre-sieving
∑ (log p)/pWeighted prime sums appearing in Chebyshev-style arguments and smooth-number estimates
e−γ correctionExplains why the sieve of Eratosthenes retains about e−γ times the naively predicted count

Design consequence

Pre-sieving candidates by all primes below B removes a fraction 1 − ∏p ≤ B(1−1/p) ≈ 1 − e−γ/ln B of them. Going from B = 100 to B = 106 only improves the survival rate from roughly 12% to 4%, because the product decays like 1/ln B. There is a clear point of diminishing returns, and it is reached early.

Contents

05

The prime number theorem and beyond

Theorem T3

Prime number theorem

π(x) ~ x/ln x, equivalently ψ(x) ~ x, equivalently pk ~ k ln k

Proved independently by Hadamard and de la Vallée Poussin in 1896 using the non-vanishing of ζ(s) on the line Re(s) = 1. Elementary proofs were found by Erdős and Selberg in 1949.

The sharper statement with the logarithmic integral, π(x) = li(x) + E(x), is where error terms live. Unconditionally E(x) = O(x·exp(−c√(ln x))); under the Riemann hypothesis E(x) = O(√x ln x), which is essentially best possible.

Theorem T4

Primes in arithmetic progressions

Dirichlet (1837): if gcd(a,d) = 1 then the progression a, a+d, a+2d, … contains infinitely many primes. PNT for progressions: the primes are asymptotically equidistributed among the φ(d) admissible residues, so π(x; d, a) ~ π(x)/φ(d).

Why this matters for structured primes

Cryptographic protocols often require primes of a special shape — safe primes p = 2q+1, or primes in a fixed residue class such as p ≡ 3 (mod 4) for fast square roots. Equidistribution says that fixing a residue class modulo a small d costs only a factor φ(d) in search time. Safe primes are a different matter: their density is conjectural, following a Hardy–Littlewood heuristic of order x/(ln x)2, and generation is correspondingly slower.

Contents

06

Gaps, and what remains open

Gap and distribution facts
StatementStatusPractical reading
Average gap near x is ~ ln xTheorem (PNT)Expected trials per prime found in key generation
Arbitrarily long prime-free gaps existTheorem — n!+2, …, n!+nNo guaranteed prime in a fixed short interval
A prime exists in (n, 2n]Theorem (Bertrand)Every bit-length is achievable
A prime exists in (n, n + n0.525] for large nTheorem (Baker–Harman–Pintz, 2001)Incremental search from a random start is provably efficient for large candidates
A prime exists between consecutive squaresOpen (Legendre's conjecture)Do not design an algorithm that requires it
Infinitely many twin primesOpen; bounded gaps below 246 known (Zhang, Maynard, Polymath)Twin-prime-based parameters have no proven density
Riemann hypothesisOpenSharp error terms in π(x) and in many algorithm analyses are conditional on it

Conditional results in algorithm analysis

Several bounds quoted in the algorithms literature — deterministic Miller–Rabin with a small fixed base set, bounds on the least primitive root, some index-calculus analyses — are conditional on the extended Riemann hypothesis. When such a bound is load-bearing for security or correctness, it must be labelled as conditional rather than presented as proved.

Contents

07

The sieve of Eratosthenes

For listing all primes below a bound — the standard way to build the small-prime table used in pre-sieving and in factor bases — nothing beats the classical sieve.

Sieve of Eratosthenes

  1. input: bound n
  2. A[2..n] ← true
  3. for p = 2 to ⌊√n⌋:
  4. if A[p]:
  5. for m = p², p²+p, p²+2p, …, ≤ n: A[m] ← false
  6. output {p : A[p]}

Θ(n log log n) operations and n bits of memory. Starting the inner loop at p² rather than 2p is what removes the redundant work; the log log factor is exactly Mertens' sum ∑ 1/p.

  • Segmented sieving processes the range in cache-sized blocks, keeping only the primes up to √n in memory. This is the practical form for n beyond a few billion.
  • Wheel factorisation skips multiples of the first few primes by construction, typically saving a constant factor of 3–4 in both time and space.
  • Linear sieves reach Θ(n) by marking each composite exactly once via its smallest prime factor, and can compute μ, φ and the factorization of every n in the range at the same time.
  • Do not use a sieve to test one large number for primality — that is the job of Miller–Rabin. The sieve answers a bulk question, not a point question.
Contents

08

Quick reference and FAQ

Density estimates for key generation
Bit length kDensity among odd k-bit integersExpected candidates per primeNotes
256≈ 1/89≈ 89Elliptic-curve field sizes
512≈ 1/178≈ 178Factor of a 1024-bit RSA modulus
1024≈ 1/355≈ 355Factor of a 2048-bit RSA modulus
1536≈ 1/532≈ 532Factor of a 3072-bit RSA modulus
2048≈ 1/710≈ 710Factor of a 4096-bit RSA modulus

Using 2/(k ln 2) for odd candidates. Trial division by primes below 1000 typically removes about 80% of candidates before the first Miller–Rabin round, cutting real cost by roughly a factor of five.

Is x/ln x good enough for planning purposes?
For estimating trial counts, yes — it is accurate to a few per cent at cryptographic sizes and errs on the conservative side. For counting primes in a range exactly, use li(x) differences.
Does the prime number theorem guarantee a prime in any given interval?
No. It is an asymptotic statement about counts, not a guarantee for a specific short interval. Bertrand's postulate gives a guarantee in (n,2n], and unconditional short-interval results reach n0.525, but nothing is proved for arbitrarily short intervals.
Are primes generated by incremental search from a random point safe to use?
In practice yes, and it is faster than resampling because pre-sieving state is reused. The distribution is slightly non-uniform — primes following a long gap are more likely to be selected — but no attack exploiting this is known for standard parameter sizes. Where uniformity is required by a specification, resample instead.
Why do safe primes take so much longer to generate?
You need q and 2q+1 both prime. Heuristically the density of such q falls off like 1/(ln x)2, so the search costs roughly a factor ln x more — hundreds of times slower at 2048 bits. Their density has never been proved to be positive, only conjectured.
Contents

10

References and further reading

  • V. Shoup, A Computational Introduction to Number Theory and Algebra, Cambridge University Press, 2005 — Chapter 5.
  • T. M. Apostol, Introduction to Analytic Number Theory, Springer, 1976 — Chapters 3, 4 and 13.
  • R. Crandall and C. Pomerance, Prime Numbers: A Computational Perspective, 2nd ed., Springer, 2005 — Chapter 1 and §3.2 on sieving.
  • R. C. Baker, G. Harman and J. Pintz, 'The difference between consecutive primes, II', Proc. London Math. Soc. 83 (2001) 532–562.
  • M. Deléglise and J. Rivat, 'Computing π(x): the Meissel, Lehmer, Lagarias, Miller, Odlyzko method', Math. Comp. 65 (1996) 235–245.

KEVOS® Knowledge LibraryEngineering → MathematicsTaxonomy ID: ENG-MATHPage ID: distribution-of-primesReview cycle: annual


Continue learning

Arithmetic Functions and Möbius InversionArticle · MathematicsNEXT LESSON →Abelian Groups and Cyclic StructureArticle · MathematicsEuler's Phi Function and Fermat's Little TheoremArticle · MathematicsRings, Ideals and Quotient RingsArticle · Mathematics