# Prime counting function

In mathematics, the prime counting function is the function counting the number of prime numbers less than or equal to some real number x. It is denoted by ${\displaystyle \pi (x)}$ (although it has no connection with the number π).

## History

Of great interest in number theory is the growth rate of the prime counting function. It was conjectured in the end of the 18th century by Gauss and by Legendre to be approximately

${\displaystyle x/\operatorname {ln} (x)}$,

in the sense that

${\displaystyle \lim _{x\rightarrow +\infty }{\frac {\pi (x)}{x/\operatorname {ln} (x)}}=1}$.

This statement is the prime number theorem. An equivalent statement is

${\displaystyle \lim _{x\rightarrow +\infty }\pi (x)/\operatorname {li} (x)=1}$ ,

where li is the logarithmic integral function. This was first proved around 1896 by Hadamard and by de la Vallée Poussin (independently), using properties of the zeta function introduced by Riemann in 1859.

More precise estimates of ${\displaystyle \pi (x)}$ are now known; for example

${\displaystyle \pi (x)=\operatorname {li} (x)+O\left(x\exp \left(-{\frac {\sqrt {\ln(x)}}{15}}\right)\right)}$,

where the O is big O notation. Proofs of the prime number theorem not using the zeta function or complex analysis were found around 1948 by Atle Selberg and by Paul Erdős (for the most part independently).

## Algorithms for evaluating π(x)

A simple way to find ${\displaystyle \pi (x)}$, if ${\displaystyle x}$ isn't too large, is to use the sieve of Eratosthenes to produce the primes smaller or equal to ${\displaystyle x}$ and then to count them.

A more elaborate way of finding ${\displaystyle \pi (x)}$ is due to Legendre: given ${\displaystyle x}$, if ${\displaystyle p_{1},p_{2},\ldots ,p_{k}}$ are distinct prime numbers, then the number of integers smaller or equal to ${\displaystyle x}$ which are divisible by no ${\displaystyle p_{i}}$ is

${\displaystyle \lfloor x\rfloor -\sum _{i}\left\lfloor {\frac {x}{p_{i}}}\right\rfloor +\sum _{i,

(where ${\displaystyle \lfloor \cdot \rfloor }$ denotes the floor function). This number is therefore equal to

${\displaystyle \pi (x)-\pi \left({\sqrt {x}}\right)+1}$

when the numbers ${\displaystyle p_{1},p_{2},\ldots ,p_{k}}$ are the prime numbers smaller or equal to ${\displaystyle {\sqrt {x}}}$.

In a series of articles published between 1870 and 1885, Ernst Meissel described (and used) a pratical combinatorial way of evaluating ${\displaystyle \pi (x)}$. Let ${\displaystyle p_{1},p_{2},\ldots ,p_{n}}$ be the first ${\displaystyle n}$ primes and denote by ${\displaystyle \Phi (m,n)}$ the number of natural numbers not greater than ${\displaystyle m}$ which are divisible by no ${\displaystyle p_{i}}$. Then

${\displaystyle \Phi (m,n)=\Phi (m,n-1)-\Phi \left(\left[{\frac {m}{p_{n}}}\right],n-1\right)}$.

Given a natural number ${\displaystyle m}$, if ${\displaystyle n=\pi \left({\sqrt[{3}]{m}}\right)}$ and if ${\displaystyle \mu =\pi \left({\sqrt {m}}\right)-n}$, then

${\displaystyle \pi (m)=\Phi (m,n)+n(\mu +1)+{\frac {\mu ^{2}-\mu }{2}}-1-\sum _{k=1}^{\mu }\pi \left({\frac {m}{p_{n+k}}}\right)}$

Using this approach, Meissel computed ${\displaystyle \pi (x)}$, for ${\displaystyle x}$ equal to 5×105, 106, 107, and 108.

In 1959, Derrick Henry Lehmer extended and simplified Meissel's method. Define, for real ${\displaystyle m}$ and for natural numbers n, and k, ${\displaystyle P_{k}(m,n)}$ as the number of numbers not greater than m with exactly k prime factors, all greater than ${\displaystyle p_{n}}$. Furthermore, set ${\displaystyle P_{0}(m,n)=1}$. Then

${\displaystyle \Phi (m,n)=\sum _{k=0}^{+\infty }P_{k}(m,n)}$,

where the sum actually has only finitely many nonzero terms. Let ${\displaystyle y}$ denote an integer such that ${\displaystyle {\sqrt[{3}]{m}}\leq y\leq {\sqrt {m}}}$, and set ${\displaystyle n=\pi (y)}$. Then ${\displaystyle P_{1}(m,n)=\pi (m)-n}$ and ${\displaystyle P_{k}(m,n)=0}$ when ${\displaystyle k\geq 3}$. Therefore

${\displaystyle \pi (m)=\Phi (m,n)+n-1-P_{2}(m,n)}$.

The computation of ${\displaystyle P_{2}(m,n)}$ can be obtained this way:

${\displaystyle P_{2}(m,n)=\sum _{y.

On the other hand, the computation of ${\displaystyle \Phi (m,n)}$ can be done using the following rules:

1. ${\displaystyle \Phi (m,0)=\lfloor m\rfloor }$;
2. ${\displaystyle \Phi (m,b)=\Phi (m,b-1)-\Phi \left({\frac {m}{p_{b}}},b-1\right)}$.

Using his method and an IBM 701, Lehmer was able to compute ${\displaystyle \pi \left(10^{10}\right)}$.

## Other prime counting functions

Other prime counting functions are also used because they are more convenient to work with. One is Riemann's prime counting function, denoted ${\displaystyle \Pi (x)}$ or ${\displaystyle J(x)}$. This has jumps of 1/n for prime powers pn, with it taking a value half-way between the two sides at discontinuities. That added detail is because then it may be defined by an inverse Mellin transform. Formally, we may define J by

${\displaystyle J(x)={\frac {1}{2}}\left(\sum _{p^{n}

where p is a prime.

We may also write

${\displaystyle J(x)=\sum _{n=1}^{\infty }\pi (x^{\frac {1}{n}})}$

except where we have discontinuities at prime powers, and hence π can be recovered from J by Möbius inversion.

Another prime counting function weights prime powers pn by ln p:

${\displaystyle \psi (x)={\frac {1}{2}}\left(\sum _{p^{n}

## Formulas for prime counting functions

These come in two kinds, arithmetic formulas and analytic formulas. The latter are what allow us to prove the prime number theorem. From the work of Riemann and von Mangoldt, we have the following expression for J:

${\displaystyle J(x)=\operatorname {Li} (x)-\sum _{\rho }\operatorname {Li} (x^{\rho })-\ln 2+\int _{x}^{\infty }{\frac {dt}{t(t^{2}-1)\ln t}}}$

Here Li is the offset logarithmic integral, and the ρ are the zeros of the Riemann zeta function in the critical strip, where the real part of ρ is between zero and one. The formula is valid for values of x greater than one, which is the region of interest, and the sum over the roots is conditionally convergent, and should be taken in order of increasing absolute value of the imaginary part.

For ψ we have a simpler formula, due to von Mangoldt:

${\displaystyle \psi (x)=x-\sum _{\rho }{\frac {x^{\rho }}{\rho }}+\ln {\frac {x^{2}}{x^{2}-1}}-\ln {2\pi }.}$

Again, the formula is valid for x > 1.

## Inequalities

Here are some useful inequalities for π(x).

${\displaystyle \pi (x)>{\frac {x}{\log x}}}$ for x ≥ 17.
${\displaystyle \pi (x)<1.25506{\frac {x}{\log x}}}$ for x > 1.
${\displaystyle {\frac {x}{\log x+2}}<\pi (x)<{\frac {x}{\log x-4}}}$ for x ≥ 55.

Here are some inequalities for the nth prime, pn.

${\displaystyle n\ \ln n+n\ln \ln n-n for n ≥ 6.

The left inequality holds for n ≥ 1 and the right inequality holds for n ≥ 6.

An approximation for the nth prime number is

${\displaystyle p_{n}=n\ln n+n\ln \ln n-n+{\frac {n\ln \ln n-2n}{\ln n}}+O\left({\frac {n(\ln \ln n)^{2}}{(\ln n)^{2}}}\right)}$

## The Riemann hypothesis

The Riemann hypothesis is equivalent to a much sharper bound on the error in the estimate for ${\displaystyle \pi (x)}$, and hence to a more regular distribution of prime numbers,

${\displaystyle \pi (x)=\operatorname {li} (x)+O({\sqrt {x}}\log {x}).}$

## References

• Eric Bach and Jeffrey Shallit, Algorithmic Number Theory, volume 1, 1996, MIT Press. ISBN 0-262-02405-5, see page 234 in section 8.8.
• Leonard Eugene Dickson, History of the Theory of Numbers I: Divisibility and Primality, 2005, Dover Publications. ISBN 0-486-44232-2