Research shelf / Mathematics / Prime meta-pattern

Mathematics

What a neural network learns about primes, and why it still loses

Train a network to tell primes from composites, then refuse to look at the feature-generating code and work out what it learned from the weights alone. The answer is the small-prime trial-division sieve on 6k±1 candidates, in that order, with strikingly stable feature importances across five orders of magnitude. The follow-up paper then benchmarks the network against the thing it rediscovered, and reports the loss.

Result-bearing AGPL-3.0+ / commercial
Evidence level

Experiments were run and the numbers are reported here.

FolderPrime Number Generator
FieldMathematics
StatusResult-bearing. Full report set, reproducible benchmarks, negative result reported.
What it is

Six MLPs trained to classify primes across five orders of magnitude, interpreted from weights alone — they rediscover trial division on the 6k±1 lattice, and then lose to it by 30–80×.

Six multilayer perceptrons are trained at scales s = log₁₀ n ∈ {3,…,8} on a deliberately rich, redundant 105-dimensional feature set. Interpretation uses two independent routes: black-box weight analysis (singular-value spectra, Hill exponents, effective rank, integrated-gradient attribution by feature group) and knowledge distillation into decision trees and sparse L1 logistic regressions.

Both routes converge on the same function. The top features, in order and at every scale, are is_6k_pm1, then n mod 5, 7, 11, 13, 17, 19 — the small-prime trial-division sieve, restricted to the 6k±1 lattice on which all primes above 3 live. Two clean exponential scaling laws fall out: the residue-feature attribution share decays as 0.543·exp(−0.041·s) while the binary-bit share grows as 2.23·exp(0.219·s), with Hill α ≈ 3.19 constant throughout.

The second paper operationalises the finding into three generators and benchmarks them head-to-head. The conventional baseline (6k±1 enumeration, small-prime pre-filter, Sorenson–Webster deterministic Miller–Rabin) is exactly correct and fastest. The NN-augmented variant is also exactly correct — the verifier guarantees it — and 30–80× slower. The pure-NN variant is not a primality test at all. The conclusion the paper draws is that the network’s value here is interpretive, not generative.

This is a negative result, published as one. The operational conclusion in the paper’s own words is that the conventional sieve-plus-Miller-Rabin pipeline strictly dominates the neural variants on speed and correctness. The work is still worth reading, because "gradient descent reliably converges on the right structural family, and here is the scaling law for which part it emphasises at which magnitude" is a real finding. It is just not the finding a headline would have wanted.
Claims ledger

Every number, and what stands behind it

A claim is only worth the evidence attached to it. Each row below carries its basis: measured on the author’s own hardware, derived from the construction, measured on synthetic data, projected from literature, or simply cited.

Breakdown of this page’s claims by what stands behind each one
scroll to see the whole chart →
Every claim, weighted by its evidence. The table below is the same data row by row.
ClaimFigureBasisContext
Function recovered by distillationsmall-prime sieve on 6k±1MeasuredSame feature ordering at every scale s = 3…8
Residue-attribution scaling law0.543 · exp(−0.041 · s)MeasuredIntegrated-gradient attribution by feature group
Binary-bit attribution scaling law2.23 · exp(0.219 · s)MeasuredSame analysis, opposite direction
Hill tail exponentα ≈ 3.19MeasuredConstant across all six scales
NN-augmented generator speed30–80× slowerMeasured50 random starts × 5 consecutive primes per scale
Pure-NN primality recall0.21–0.68MeasuredAt τ = 0.5; skip rate 2–22%
Verifier exactness boundexact for n < 3.317 × 10²⁴CitedSorenson–Webster deterministic Miller–Rabin
Probabilistic error above that bound≈9.1 × 10⁻¹³ per callDerivedk = 20 rounds, 4⁻²⁰
Cross-study agreementevery cross-checkable claimMeasuredAgainst an independent non-NN baseline study

Measured — author-run experiment on the stated setup. Synthetic — measured, but on synthetic rather than real data. Derived — follows from the stated construction or proof. Projected — paper-stated projection, not an author-run benchmark. Cited — taken from external literature.

Interactive

Run the sieve the network rediscovered

Three generators scan the same window of consecutive integers. The conventional one walks the 6k±1 lattice, trial-divides by small primes, and verifies with Miller–Rabin. The augmented one swaps the trial division for the distilled scorer and keeps the verifier. The third uses the scorer alone. Move the scale up and watch which one stops working.

6k±1 sieve — live in your browser

Off the 6k±1 lattice Rejected Prime, scorer agreed Prime, scorer missed it Composite, scorer accepted
 ConventionalNN-augmentedPure NN
Primes found — — —
Miller–Rabin calls — — 0
Primes skipped — — —
Filter cost per candidate — — —
False positives None None Recall —
Trial divisors used —
Composites accepted —
Verifier calls saved —
Filter cost ratio —

 

The paper reports the NN-augmented generator running 30–80× slower than the conventional one — MLP inference per candidate, with no matching reduction in candidate count — and pure-NN primality recall of 0.21–0.68 at τ = 0.5. The ratio here is larger because it isolates the filter; the paper’s figure is end-to-end, with the verifier in both denominators. This page reproduces the mechanism, not the figures.
Miller–Rabin here is the real thing — the first thirteen primes as witnesses, deterministic below 3.317 × 1024, so every “prime” on this page is exactly that. The scorer is a real 105 → 64 → 1 forward pass over the same deliberately redundant feature set the paper used, but its first layer is hand-set rather than trained: twelve units form clipped ramps reading zero when p divides n, for p in {5, 7, 11, 13, 17, 19}, weighted by −log(1 − 1/p). That is trial division written as a likelihood ratio — the function Paper 1 distilled out of the trained weights. The other fifty-two units carry the redundant features at small random weights; they are what costs the network its recall, and what costs it its speed.
Methods

How it works

  • Weights-only interpretation. The feature-generating source is deliberately not consulted; only trained weights and gradients are used.
  • Two independent extraction routes. Spectral weight analysis and distillation into trees and sparse logistic models, cross-checked against each other.
  • Head-to-head generator benchmark. Three generators at six scales, 50 random starts × 5 consecutive primes each.
  • Deterministic verification. Sorenson–Webster Miller–Rabin, exact below 3.317 × 10²⁴, with k = 20 probabilistic rounds above.
Stated limitations

What it does not do

Taken from the folder’s own README. Nothing here has been softened.

  • The network does not discover a prime formula. No such formula is known and the prime-counting function is provably non-elementary — the paper says so in its own setup.
  • The pure-NN generator has recall between 0.21 and 0.68. It is not a primality test and is not presented as one.
  • The NN-augmented generator is slower than the classical one it was built to improve, by 30–80×.
  • Distillation recovers what the features made available. A different feature set would recover a different function.
  • Scales stop at s = 8. Behaviour at cryptographic sizes is untested and not claimed.
Use it

Free under AGPL-3.0+ for almost everyone

Personal use, charities, education and organisations under AUD 50,000 a year pay nothing. A tiered commercial licence covers everyone else.