How many primes are there up to x
The prime numbers look scattered at random: there are $25$ up to $100$, $168$ up to $1{,}000$, $1{,}229$ up to $10{,}000$. Nobody can say, without testing, whether $1{,}000{,}003$ is prime. But seen from a distance they have a regularity. Writing $\pi(x)$ for the number of primes up to $x$, Legendre’s question was: is there a simple formula that gives $\pi(x)$ approximately?
The formula of 1798
In the first edition of the Essay on the Theory of Numbers, Legendre compared the available tables of primes with various expressions and proposed
$$\pi(x) \approx \frac{x}{A \ln x + B}.$$In the second edition, of 1808, now with tables up to a million, he fixed the values $A = 1$ and $B = -1.08366$:
$$\pi(x) \approx \frac{x}{\ln x - 1.08366}.$$The fit is astonishing. For $x = 10{,}000$, $\ln x \approx 9.2103$, and the formula gives
$$\frac{10{,}000}{9.2103 - 1.08366} = \frac{10{,}000}{8.1267} \approx 1{,}230.5,$$against the true value $\pi(10{,}000) = 1{,}229$. For $x = 10^6$ it gives about $78{,}543$, against $78{,}498$. The simpler formula $x/\ln x$ gives only $72{,}382$.
The figure shows the error of each estimate, that is, the difference between it and the true number of primes:
Gauss, again
Gauss related, in an 1849 letter to Encke, that around 1792, at fifteen, he had arrived at a similar estimate by studying tables of primes: near $x$, the “density” of the primes is about $1/\ln x$. Adding up this density gives the logarithmic integral
$$\operatorname{Li}(x) = \int_2^x \frac{dt}{\ln t}.$$Up to a million, Legendre’s formula is better than Gauss’s: $\operatorname{Li}(10^6) \approx 78{,}626$, which is off by about $128$, while Legendre is off by $45$. But the constant $1.08366$ was fitted to the tables Legendre had. In 1849 Chebyshev proved that if the number that makes the formula $x/(\ln x - B)$ work tends to some limit as $x$ grows, that limit is $1$, not $1.08366$. For very large $x$, Legendre’s formula is worse than $\operatorname{Li}(x)$.
What the two formulas have in common is the claim that
$$\lim_{x\to\infty} \frac{\pi(x)}{x/\ln x} = 1.$$This is the prime number theorem. Legendre and Gauss guessed it; Riemann, in 1859, showed the way; Hadamard and de la Vallée Poussin proved it in 1896.
Legendre’s sieve
Legendre also gave an exact formula for counting primes, an algebraic version of the sieve of Eratosthenes. To count the numbers up to $x$ that are not divisible by any of the primes $p_1, \ldots, p_k$ up to $\sqrt{x}$, start with all of them, remove the multiples of each prime, put back those removed twice, remove those put back too often, and so on:
$$x - \sum_i \left\lfloor \frac{x}{p_i} \right\rfloor + \sum_{iThese $22$ numbers are $1$ and the primes from $11$ to $97$. Adding back the four primes $2, 3, 5, 7$ and removing the $1$, $\pi(100) = 22 + 4 - 1 = 25$. The formula is exact, but it has one term for each product of primes up to $\sqrt{x}$, and for large $x$ it becomes impractical. Meissel, in 1870, and Lehmer, in 1959, reorganised it to count the primes up to billions.
Exercise: Legendre's formula up to 100,000
Compute Legendre’s estimate for $x = 100{,}000$ and compare it with $\pi(100{,}000) = 9{,}592$.
$\ln 100{,}000 = 5 \ln 10 \approx 11.5129$. Then
$$\frac{100{,}000}{11.5129 - 1.08366} = \frac{100{,}000}{10.4293} \approx 9{,}588,$$an error of only $4$ primes, or $0.04\%$. The formula $x/\ln x$ gives $8{,}686$, an error of almost $10\%$.
Exercise: how many zeros does 100! have?
In the Essay, Legendre also gave the formula for the highest power of a prime $p$ that divides $n!$: the exponent is
$$\left\lfloor \frac{n}{p} \right\rfloor + \left\lfloor \frac{n}{p^2} \right\rfloor + \left\lfloor \frac{n}{p^3} \right\rfloor + \cdots$$Use it to find how many zeros $100!$ ends with.
Each final zero comes from a factor $10 = 2 \cdot 5$, and there are many more factors $2$ than $5$. It is enough to count the $5$s: $\left\lfloor \frac{100}{5} \right\rfloor + \left\lfloor \frac{100}{25} \right\rfloor = 20 + 4 = 24$, and $125 > 100$. The number $100!$ ends with $24$ zeros. The first term counts the multiples of $5$, the second counts a second time the multiples of $25$, which have two factors $5$.
Sources: Adrien-Marie Legendre, Essai sur la théorie des nombres (1798 and 1808) · Letter from Gauss to Encke, 24 December 1849, in Werke II · Pafnuty Chebyshev, “Sur la fonction qui détermine la totalité des nombres premiers inférieurs à une limite donnée” (1849) · Hans Riesel, Prime Numbers and Computer Methods for Factorization (1994).
All topics on Adrien-Marie Legendre · ← Quadratic reciprocity · The method of least squares →