🔷 Prime Distribution & Gaps

Visualize prime number distribution: spiral (Ulam), prime gaps histogram, and the Prime Number Theorem π(n) ≈ n/ln(n). See Bertrand's postulate and twin prime gaps interactively.

MathematicsInteractive
Switch view mode to explore different visualizations · R redraw

How it Works

This simulator generates all primes up to N using the Sieve of Eratosthenes, then visualizes their distribution in four different ways. Each view reveals different properties of the prime numbers.

The Ulam Spiral (1963) arranges integers on a square spiral, coloring primes. Diagonal patterns emerge because many prime-rich polynomials (like 4n²-2n+1) lie along these diagonals. The Gap Histogram shows the distribution of distances between consecutive primes. The PNT view plots π(x) vs. x/ln(x) to demonstrate the Prime Number Theorem approximation.

Sieve: composite[k·p] = true for k = 2,3,... and each prime p
PNT: π(n) ~ n/ln(n)    (Hadamard & de la Vallée Poussin, 1896)
Gap: g(p) = next_prime(p) - p,   avg g ≈ ln(p)
Bertrand: ∀ n>1, ∃ prime p : n < p ≤ 2n

The Sieve of Eratosthenes runs in O(N log log N) time and O(N) space — efficient enough to find all primes up to 50,000 in milliseconds. After sieving, gap statistics, twin prime counts, and the PNT approximation ratio are computed.

Frequently Asked Questions

What is the Ulam spiral?

The Ulam spiral (1963) places integers on a square spiral starting from the center, then highlights primes. Primes tend to cluster along diagonal lines, suggesting arithmetic progressions contain unusually many primes. This pattern remains partially unexplained.

What is the Prime Number Theorem?

The Prime Number Theorem states that π(n) ~ n/ln(n) as n→∞. Equivalently, the average gap between consecutive primes near n is approximately ln(n). Proved independently by Hadamard and de la Vallée Poussin in 1896.

What are twin primes?

Twin primes are pairs of primes differing by 2: (3,5), (11,13), (17,19), (29,31)... The Twin Prime Conjecture states there are infinitely many such pairs, but it remains unproven. Yitang Zhang proved in 2013 there are infinitely many prime pairs differing by at most 70 million.

What is Bertrand's postulate?

Bertrand's postulate (proved by Chebyshev 1852): for every n > 1, there exists at least one prime p with n < p ≤ 2n. This means prime gaps can never be larger than the primes themselves in a relative sense.

What is the Riemann Hypothesis connection to primes?

The Riemann Hypothesis conjectures that all non-trivial zeros of ζ(s) lie on Re(s) = 1/2. If true, it gives the best possible error bound: |π(x) - Li(x)| ≤ C·√x·ln(x), where Li(x) is the logarithmic integral.

What is the Sieve of Eratosthenes?

The Sieve of Eratosthenes finds all primes up to n: start with all integers 2…n; repeatedly mark multiples of each prime as composite. Primes are the survivors. Time complexity is O(n log log n). Used here to generate the prime list.

What is the prime gap distribution?

The gap between consecutive primes p and q is q-p. Cramér's conjecture predicts the maximal gap near n is approximately (ln n)². The distribution of normalized gaps g/ln(p) follows an exponential distribution, consistent with primes behaving like a Poisson process.

What is Dirichlet's theorem on primes in arithmetic progressions?

For coprime integers a and d, the arithmetic progression a, a+d, a+2d, … contains infinitely many primes, distributed roughly equally among the φ(d) valid residue classes mod d. This explains diagonal patterns in the Ulam spiral.

What is the Green-Tao theorem?

The Green-Tao theorem (2004) proves that the primes contain arithmetic progressions of arbitrary length — for any k, there exist k primes a, a+d, …, a+(k-1)d. Uses techniques from ergodic theory.

How dense are the primes?

The density of primes near n is approximately 1/ln(n) by the PNT. A random integer near n is prime with probability ~1/ln(n). Density decreases as n grows, but there are always infinitely many primes (Euclid, ~300 BCE).

About this simulation

This tool sieves every prime up to N (up to 50,000) with the Sieve of Eratosthenes and renders the results four ways: as an Ulam spiral, a gap histogram, a Prime Number Theorem overlay, and a gap scatter plot. Switching views reveals different structure in the same underlying prime list — from the mysterious diagonal clustering in the spiral to how closely π(x) tracks x/ln(x).

🔬 What it shows

Four linked visualizations of the primes below N: the Ulam spiral coloring primes on a square spiral, a histogram of gaps between consecutive primes, an overlay comparing π(x) to the PNT approximation x/ln(x), and a scatter of gap size vs. prime value with an ln(p) reference curve.

🎮 How to use

Pick a view with View mode, set Max N (1,000-50,000), adjust Spiral cell size for the Ulam view, and use Highlight to color twin primes, primes ≡1 (mod 4), or primes near powers of 2. The Statistics panel tracks π(N), N/ln(N), max gap, and twin pair count live.

💡 Did you know?

Stanislaw Ulam discovered the diagonal-clustering pattern in 1963 while doodling during a boring conference talk — sketching integers on a spiral and circling the primes almost by accident revealed one of number theory's enduring visual mysteries.

Frequently asked questions

Why do the diagonal lines appear in the Ulam spiral?

Many quadratic polynomials like 4n²-2n+1 generate disproportionately many primes, and these polynomials correspond to diagonal lines on the spiral. The effect is a real statistical bias, though a complete explanation remains an open problem in number theory.

Why does the orange bar dominate the low end of the Gap Histogram?

Gap 2 (twin primes) and other small gaps are far more common than large gaps because, per the Prime Number Theorem, the average gap near a prime p grows like ln(p) — for small primes that average is tiny, so small gaps vastly outnumber large ones.

Why do π(x) and x/ln(x) drift apart as Max N increases?

The Prime Number Theorem is an asymptotic statement — the ratio π(x)/(x/ln(x)) approaches 1 as x→∞, but at finite N there's a persistent gap. A better approximation is the logarithmic integral Li(x), which the Riemann Hypothesis would bound tightly.

What does the pink ln(p) line mean in the Gap scatter view?

It's the theoretical average prime gap near value p, predicted by the Prime Number Theorem. Individual gaps scatter widely above and below this curve, but their average tracks it closely, and Cramér's conjecture predicts the largest gaps grow like (ln n)².

Why does raising Max N make the simulation slower?

The Sieve of Eratosthenes runs in O(N log log N) time, so larger N means more numbers to sieve and more primes to draw. At 50,000 it's still fast (milliseconds), but rendering the Ulam spiral with a small cell size at high N draws far more pixels per frame.