Number

The first number that is not a square

Modulo a prime, half the numbers are perfect squares. Count up from 1 and the first non-square is usually 2 or 3 — but modulo 366,791 the numbers 1 to 42 are all squares. How far the run of squares can last is one of the oldest open questions about primes: the truth is tiny, the proofs are huge, and the Riemann hypothesis would close most of the gap.

Worth reading first: Counting one rectangle, twice · The two supplements, and where the eight comes from.

Modulo an odd prime pp, exactly half of the numbers 1,2,…,p−11, 2, \ldots, p - 1 are perfect squares — quadratic residues — and the other half are not. Reciprocity says whether one prime is a square modulo another, and its supplements settle −1 and 2. Those laws decide whether a particular number is a square. This essay asks about the first number that is not.

For half of all primes the answer is 2. For most of the rest it is 3 or 5. But there are primes for which the squares run on for a long time: modulo 366,791, every number from 1 to 42 is a square, and 43 is the first that is not.

Forty-two squares in a row. p = 366791: partial sums of (n/p) for n ≤ 200: 0, 10, 20, 30, 40, 46, 54, 60, 64, 72, 78, 78, 88, 92, 98, 102, 108, 116, 122, 126, 132; least non-residue 43.
Fig. 1 The running sum of the Legendre symbols modulo 366,791 for n from 1 to 200 — up one step when n is a square modulo the prime, down one when it is not. The walk climbs straight for 42 steps; 43 is the first non-square.

Write n(p)n(p) for the least quadratic non-residue modulo pp. How large can it be? The truth, as far as any computation shows, is tiny — a small multiple of log⁡p\log p. The best theorem allows about p0.152p^{0.152}. The Riemann hypothesis, if true, would bring the bound down to 2(ln⁡p)22(\ln p)^2, and the gap between the conjecture and what is proved has stood since 1957.

Why anyone needs a non-square

The question is not idle, because several of the most-used algorithms in number theory need a non-square before they can start, and the only general way to find one is to try 2,3,4,…2, 3, 4, \ldots until one fails. Taking a square root modulo a prime is the standard example. When pp leaves 3 on division by 4 the root of a square aa is simply a(p+1)/4a^{(p+1)/4}, but when p−1p - 1 is divisible by a high power of 2 the Tonelli–Shanks method has to climb through the 2-power part of the group of remainders, and it does so by the powers of a single known non-square. Every step after that one is deterministic and fast. The first step is a search, and its length is n(p)n(p).

Primality testing has the same shape. A test to base aa catches a composite nn unless aa happens to be one of its liars, and for the Euler form of the test the liars of a composite make up a proper subgroup of the remainders prime to nn — at most half of them. The quadratic residues modulo a prime are a proper subgroup too, of exactly half, and n(p)n(p) is the first number outside it. So whether testing every base up to some small bound is enough to certify a prime is the same question one level up — how long can a run of numbers stay inside a proper subgroup? — and it has the same answer: under the generalised Riemann hypothesis, testing every base up to 2(ln⁡n)22(\ln n)^2 suffices, a result of exactly the kind this essay is about, and running the test backwards is how a prime is certified without it. Unconditionally the guarantees are far weaker, and in practice nobody has ever met a case where they matter.

So the gap between what is true and what is proved is not only a gap in understanding. It is the difference between an algorithm whose running time is known and one whose running time is merely observed.

Why the first non-square is a prime

The least non-residue is always a prime, and the reason is the multiplicativity of squares. A product of two squares modulo pp is a square, so a number all of whose prime factors are squares is itself a square. The first number that is not a square therefore has a prime factor that is not a square, and that prime factor is no larger than the number — so it is the number. The walk in the figure keeps climbing long after 43 for the same reason: every number built only from the primes below 43, like 44 or 45 or 48, is still a square.

Euler’s criterion makes the test concrete: aa is a square modulo pp exactly when a(p−1)/2a^{(p-1)/2} leaves 1, and is not when it leaves p−1p - 1. For p=366,791p = 366{,}791 that power of 2 leaves 1, and so does that power of 3, of 5, and of every prime up to 41; the power of 43 leaves 366,790366{,}790. Then the walk climbs again through 44, 45 and 46, because each factors into primes already shown to be squares; it turns down at 47, the second non-square prime, and then climbs straight through 48 to 52 for the same reason as before. It turns down only at a number with an odd count of non-square prime factors. By 200 it has taken 166 steps up and 34 down. The first non-square fixes the pattern of everything after it to a remarkable extent: a single multiplicative choice at each prime determines the whole walk.

So n(p)n(p) is the first prime qq that is not a square modulo pp, and reciprocity decides each prime separately. Whether 2 is a square depends on pp modulo 8; whether 3 is depends on pp modulo 12; whether 5 is depends on pp modulo 5; and in general whether qq is depends on pp modulo 4q4q. Those conditions are independent across different qq, and each holds for half of all primes, so the chance that 2, 3, 5, …, up to the (k−1)(k-1)-th prime are all squares and the kk-th is not is 2−k2^{-k}.

Half the primes stop at two. 2: 0.500114, 3: 0.250022, 5: 0.125236, 7: 0.062512, 11: 0.031122, 13: 0.015642, 17: 0.007901, 19: 0.003920, 23: 0.001931; largest value 53.
Fig. 2 The share of the 664,578 odd primes below ten million whose least non-residue is each small prime, on a logarithmic scale, against 1/2, 1/4, 1/8 and so on.

The shares match: 50.0 per cent of primes below ten million have n(p)=2n(p) = 2, 25.0 per cent have n(p)=3n(p) = 3, 12.5 per cent n(p)=5n(p) = 5, and so on down to a fifth of a per cent for 23. The prediction is exact in the limit, by Dirichlet’s theorem on primes in progressions applied to all the moduli at once, and ten million is enough to see it to three significant figures for the first several primes.

The independence is the Chinese remainder theorem at work in disguise. The conditions on pp modulo 8, modulo 12, modulo 20, modulo 28 combine into one condition modulo their least common multiple, and the remainders allowed by each combine freely; Dirichlet’s theorem then says the primes spread evenly across all the allowed remainders. Nothing in that argument is probabilistic. The primes are not random, and the 2−k2^{-k} law is a statement about how they fill arithmetic progressions, but the arithmetic makes them behave exactly as a sequence of fair coins would — heads at each small prime until the first tails.

That picture is accurate for any fixed kk, and it is precisely what fails at the extremes. For a coin, a run of kk heads has probability 2−k2^{-k} whatever kk is; for primes, a run of kk squares needs pp to sit in one of a set of progressions whose modulus is the product of the first kk primes, and there are not enough primes below xx to fill progressions with a modulus far beyond xx. So the coin model predicts a longest run of order log⁡2\log_2 of the number of primes, about log⁡p\log p — which is roughly where the records sit — but it cannot be turned into a proof, because the uniform spread of primes across progressions is only known for moduli much smaller than the primes themselves.

Reciprocity decides the first steps

The first two cases are decided by the remainder of pp on division by 24.

Reciprocity decides the first steps. 1 mod 24: 82887 primes, n(p)=2 0.000, 3 0.000; 5 mod 24: 83080 primes, n(p)=2 1.000, 3 0.000; 7 mod 24: 83070 primes, n(p)=2 0.000, 3 1.000; 11 mod 24: 83047 primes, n(p)=2 1.000, 3 0.000; 13 mod 24: 83124 primes, n(p)=2 1.000, 3 0.000; 17 mod 24: 83089 primes, n(p)=2 0.000, 3 1.000; 19 mod 24: 83113 primes, n(p)=2 1.000, 3 0.000; 23 mod 24: 83167 primes, n(p)=2 0.000, 3 0.000.
Fig. 3 The eight remainders a prime above 3 can leave on division by 24, whether 2 and 3 are squares modulo such a prime, and what that forces for the least non-residue, checked against every prime below ten million in each class.

The number 2 is a square modulo pp exactly when pp leaves 1 or 7 on division by 8, and 3 exactly when pp leaves 1 or 11 on division by 12. Combined, the eight possible remainders modulo 24 split as four where 2 is not a square and n(p)=2n(p) = 2, two where 2 is but 3 is not and n(p)=3n(p) = 3, and two — the remainders 1 and 23 — where both are squares and n(p)n(p) is at least 5. Every prime below ten million obeys the table; it is a theorem, and the check is that the arithmetic was done right. Deciding further needs remainders modulo 5, 7 and beyond, and every additional prime doubles the number of classes, which is why a prime with n(p)=43n(p) = 43 has to satisfy fourteen independent conditions at once and is correspondingly rare.

An average of 3.6746

Since n(p)n(p) is the kk-th prime with probability 2−k2^{-k}, its average over primes should be 22+34+58+716+⋯\tfrac22 + \tfrac34 + \tfrac58 + \tfrac7{16} + \cdots, the primes weighted by powers of a half. Paul Erdős proved in 1961 that it is.

An average that climbs to 3.6746. x=10^3.90: 3.46640; x=10^5.88: 3.64151; x=10^6.20: 3.64804; x=10^6.39: 3.65471; x=10^6.53: 3.65657; x=10^6.63: 3.65754; x=10^6.71: 3.65887; x=10^6.79: 3.66105; x=10^6.85: 3.66001; x=10^6.90: 3.66123; x=10^6.95: 3.66247; x=10^7.00: 3.66336; limit 3.674643966.
Fig. 4 The average of the least quadratic non-residue over all odd primes up to x, for x up to ten million, against Erdős’s limit ∑pk/2k\sum p_k/2^k.

The first term contributes 1, the first two 1.75, the first three 2.375, the first five 3.156; by the ninth prime, 23, the partial sum is 3.611, and it takes twenty terms to get within a thousandth of the limit. So the average is dominated by the small primes, as the distribution says it must be, but the tail matters at the second decimal place. The sum is 3.674643966…3.674643966\ldots and the average over primes below ten million is 3.6634, still creeping upward. The approach is slow because the large values of n(p)n(p) — which contribute to the average in proportion to their size — are rare among small primes and only begin to appear in their limiting proportion when the primes are large enough to accommodate long runs of squares. A prime with n(p)n(p) equal to the kk-th prime has to lie in a particular residue class modulo the product of 4q4q over the first kk primes, and below ten million there is not room for most of those classes to be filled.

Records far below every bound

The averages describe typical primes. The extreme primes are what the theorems are about.

Records far below every bound. Record least non-residues: 2 at 3, 3 at 7, 5 at 23, 7 at 71, 11 at 311, 13 at 479, 17 at 1559, 19 at 5711, 23 at 10559, 29 at 18191, 31 at 31391, 43 at 366791, 47 at 3818929, 53 at 9257329.
Fig. 5 The largest least non-residue among primes up to p, as p runs to ten million, on logarithmic scales, against the bound 2(ln⁡p)22(\ln p)^2 that would follow from the generalised Riemann hypothesis, and against ln⁡p\ln p.

The records come at 3, 7, 23, 71, 311, 479 and on through the figure, reaching 43 at 366,791, 47 at 3,818,929 and 53 at 9,257,329. Divided by ln⁡p\ln p, they run 1.5 to 1.6 for the first few, 2.0 to 2.5 for records between 311 and 10,559, and 3.0 to 3.4 for the last four. So they grow faster than ln⁡p\ln p, but only by a factor that has roughly doubled while pp has grown by five orders of magnitude. Against 2(ln⁡p)22(\ln p)^2 — which is 328 at 366,791 and 515 at 9,257,329 — they are a tenth of the allowance or less, and the ratio is falling. Between records the gaps are irregular: no new record arrives between 31 at 31,391 and 43 at 366,791, and 37 and 41 are skipped altogether, because the first prime with a given least non-residue need not come before the first prime with a larger one. Every one lies under 2(ln⁡p)22(\ln p)^2, the explicit bound Eric Bach proved in 1990 would follow from the generalised Riemann hypothesis — Nesmith Ankeny had shown in 1952 that the hypothesis implies some bound of that shape.

Without the hypothesis the best result is David Burgess’s of 1957: n(p)n(p) is at most about p1/(4e)≈p0.152p^{1/(4\sqrt e)} \approx p^{0.152} for large pp, up to factors smaller than any power. It is a deep theorem — the exponent comes from combining a careful estimate of how evenly squares are spread in short intervals with an old trick of Vinogradov’s — and it is enormously weaker than the truth seems to be: at p=10100p = 10^{100} it allows a first non-square around 101510^{15}, where the Riemann-hypothesis bound allows about a hundred thousand and the records suggest a few hundred. Ivan Vinogradov conjectured that n(p)n(p) grows more slowly than every power of pp, and that conjecture is still open.

These are statements about the limit, and at the sizes in the figure they say almost nothing numeric. At p=9,257,329p = 9{,}257{,}329 the power p0.152p^{0.152} is only about 11, below the true value of 53; Burgess’s theorem carries an arbitrarily small extra power and an unstated constant, and it is the growth rate that is being compared, not the value. Explicit versions exist, with constants worked out, and they begin to bite only for primes with dozens of digits.

There is a lower bound too. Sidney Graham and Cecil Ringrose proved in 1990 that n(p)n(p) exceeds a constant times ln⁡p⋅ln⁡ln⁡ln⁡p\ln p \cdot \ln\ln\ln p for infinitely many primes, so the records cannot stay bounded by a fixed multiple of ln⁡p\ln p — and under the Riemann hypothesis Hugh Montgomery had shown that ln⁡p⋅ln⁡ln⁡p\ln p \cdot \ln\ln p is reached infinitely often. The true growth of the records is somewhere between those lower bounds and 2(ln⁡p)22(\ln p)^2.

A walk that stays near the axis

Burgess’s proof, like every bound on n(p)n(p), begins by controlling the walk of Legendre symbols: if the first xx numbers were all squares, the walk would climb xx steps straight up, and a theorem that the walk never climbs that far rules it out.

A walk that stays within √p log p. 233: 0.655; 4457: 0.659; 9391: 1.135; 14653: 0.677; 20089: 0.564; 25657: 0.593; 31379: 1.744; 37139: 0.950; 42961: 0.569; 49103: 1.521; 55109: 0.690; 61129: 0.550; 67391: 1.333; 73589: 0.888; 79843: 0.715; 86161: 0.576; 92459: 1.648; 98837: 0.767; 105251: 1.507; 111751: 1.705; 118213: 0.902; 124679: 1.566; 131221: 0.569; 137699: 1.051; 144379: 0.745; 150919: 1.508; 157483: 0.935; 164239: 1.564; 171053: 0.723; 177841: 0.576; 184477: 0.685; 191123: 0.892; 197893: 0.692; largest 180311: 2.068.
Fig. 6 For primes spread up to 200,000, the farthest the walk of Legendre symbols strays from nought, as a multiple of p\sqrt p, with ln⁡p/3\ln p / 3 for scale.

George Pólya and Ivan Vinogradov proved independently in 1918 that the walk never strays more than about pln⁡p\sqrt p \ln p from nought. The proof expands the indicator of an interval into sums of roots of unity weighted by the symbol — Gauss sums, whose size is exactly p\sqrt p — and the logarithm comes from adding the contributions of all the frequencies. In practice the excursions are far smaller than the theorem allows: between about half of p\sqrt p and twice it for every prime sampled. The walk looks like a random walk of ±1\pm 1 steps, whose excursions over pp steps are of order p\sqrt p, and the extra logarithm in the theorem is the price of proving it for every prime rather than typical ones.

A climb of xx straight steps at the start of the walk would be an excursion of xx, so the Pólya–Vinogradov bound alone gives n(p)≤pln⁡pn(p) \le \sqrt p \ln p. That is a genuine theorem — it is smaller than pp — but only barely, and it uses nothing about the first numbers in particular. The improvement comes from using what the first non-square forces on everything after it.

Where the square root of e comes from

Suppose every prime up to yy is a square modulo pp. Then so is every number up to xx whose prime factors are all at most yy — every yy-smooth number — and the share of numbers up to xx that are smooth is Dickman’s function ρ(u)\rho(u), where y=x1/uy = x^{1/u}. On the range from 1 to 2 that function is exactly 1−ln⁡u1 - \ln u.

Now count the non-squares up to xx another way. Each has a prime factor above yy that is a non-square, so there are at most as many as there are numbers up to xx with some prime factor between yy and xx, which is about x∑1/qx \sum 1/q over primes qq in that range — and by Mertens’s theorem that sum is ln⁡(ln⁡x/ln⁡y)=ln⁡u\ln(\ln x / \ln y) = \ln u. So at most a share ln⁡u\ln u of the numbers up to xx are non-squares.

The Pólya–Vinogradov bound says the other thing: once xx is a little larger than pln⁡p\sqrt p \ln p, the walk has not strayed far from nought, so very nearly half of the numbers up to xx are non-squares. Half cannot be at most ln⁡u\ln u unless ln⁡u≥12\ln u \ge \tfrac12, which is u≥eu \ge \sqrt e. With xx just above p\sqrt p, the least non-residue is at most y=x1/ey = x^{1/\sqrt e}, about p1/(2e)≈p0.303p^{1/(2\sqrt e)} \approx p^{0.303}.

That is Vinogradov’s argument from the 1920s, and the e\sqrt e is nothing more mysterious than the point where the smooth numbers make up exactly half: ρ(e)=1−12=12\rho(\sqrt e) = 1 - \tfrac12 = \tfrac12. Burgess’s contribution was to make the walk’s bound work on intervals of length p1/4+εp^{1/4 + \varepsilon} instead of p\sqrt p, by an argument that raises the character sum to a high even power and counts solutions of a congruence; feeding that into the same smooth-number count halves the exponent to 1/(4e)≈0.1521/(4\sqrt e) \approx 0.152. Nobody has improved the 1/41/4 in nearly seventy years, and a bound for the walk on shorter intervals is exactly what any further progress would need.

What the computations cannot settle

Every figure is exact for the primes it covers, and none says anything about larger primes. The records below ten million fit comfortably under 2(ln⁡p)22(\ln p)^2, but that bound is conditional on an unproved hypothesis, and no finite computation tests it; a prime with an enormous least non-residue, if one existed, would be a counterexample to the generalised Riemann hypothesis and could be anywhere. The distribution’s agreement with 2−k2^{-k} is a theorem in the limit, and the figure shows only how close ten million comes.

There is also a quieter limitation. The records are computed by testing 2,3,5,…2, 3, 5, \ldots in turn with the Jacobi symbol, which is exact; but listing the first prime with each value of n(p)n(p) beyond 53 needs searching far beyond ten million, where the next records lie, and the figure shows only the ones in range.

Still open: Vinogradov’s conjecture

Is n(p)<pεn(p) < p^\varepsilon for every ε>0\varepsilon > 0 and every large enough prime pp? That is Vinogradov’s conjecture, and it is open; even the much weaker statement that the exponent 1/(4e)1/(4\sqrt e) can be lowered at all has resisted every attempt since Burgess. The generalised Riemann hypothesis would give far more — a bound by a power of log⁡p\log p — and the same hypothesis would settle a whole family of related questions, like how small the least primitive root can be, the first number whose powers run through every non-zero remainder. That the least non-residue is in practice a small multiple of log⁡p\log p, while what can be proved is a fractional power of pp, makes it one of the cleanest examples of the distance between what primes evidently do and what can be shown about them.

Squares in a row

Modulo a prime, the first number that is not a square is itself a prime, and it is the kk-th prime for a fraction 2−k2^{-k} of all primes — so its average is Erdős’s ∑pk/2k=3.6746\sum p_k/2^k = 3.6746. Its largest values grow like a small multiple of ln⁡p\ln p: 43 at 366,791, 53 at 9,257,329, all beneath the bound 2(ln⁡p)22(\ln p)^2 the Riemann hypothesis would give. Unconditionally the best bound is Burgess’s p0.152p^{0.152}, built on the Pólya–Vinogradov estimate that the walk of Legendre symbols stays within pln⁡p\sqrt p \ln p of nought — a walk that in practice never strays much beyond p\sqrt p.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

ConjectureGauss sumLegendre symbolPrimesPrimitive rootQuadratic reciprocityQuadratic residueRandom walkRiemann hypothesis