{
  "id": "prime-counting-function",
  "title": "Prime-counting Function",
  "lang": "en",
  "source_lang": "en",
  "summary": "The prime-counting function counts primes not exceeding a given number and connects their distribution with asymptotic analysis, exact algorithms, and the zeros of the Riemann zeta function.",
  "category": "mathematics",
  "subcategory": "algebra",
  "type": "concept",
  "aliases": [
    "Prime counting function",
    "Prime-counting function π(x)"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/prime-counting-function/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/prime-counting-function/",
      "json": "https://aiwiki.page/zh/prime-counting-function.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "The prime-counting function, denoted by \\(\\pi(x)\\), is the number of prime numbers less than or equal to a real number \\(x\\). Thus\n\n\\[\\pi(x)=\\#\\{p:p\\text{ is prime and }p\\le x\\},\\]\n\nwhere \\(\\#\\) denotes the number of elements in a finite set. For example, \\(\\pi(10)=4\\), because the primes not exceeding \\(10\\) are \\(2,3,5,\\) and \\(7\\). It is a central object in number theory: its exact values describe the distribution of primes, while its approximations and error terms connect prime numbers with mathematical analysis and the Riemann zeta function."
    },
    {
      "heading": "Definition and elementary properties",
      "text": "The definition applies to all real \\(x\\), with \\(\\pi(x)=0\\) for \\(x<2\\). Since primes are integers,\n\n\\[\\pi(x)=\\pi(\\lfloor x\\rfloor).\\]\n\nThe function is nondecreasing and right-continuous. It is constant between consecutive primes and has a jump of size \\(1\\) at every prime.\n\nUsing an indicator function, it can be written as\n\n\\[\\pi(x)=\\sum_{1\\le n\\le x}\\mathbf 1_{\\mathbb P}(n),\\]\n\nwhere \\(\\mathbf 1_{\\mathbb P}(n)\\) equals \\(1\\) when \\(n\\) is prime and \\(0\\) otherwise. In particular, for every integer \\(n\\ge2\\),\n\n\\[\\pi(n)-\\pi(n-1)= \\begin{cases} 1,&n\\text{ is prime},\\\\ 0,&n\\text{ is composite}. \\end{cases}\\]\n\nSome exact values are:\n\n\\(x\\) | \\(\\pi(x)\\)\n\\(10\\) | \\(4\\)\n\\(100\\) | \\(25\\)\n\\(1{,}000\\) | \\(168\\)\n\\(10{,}000\\) | \\(1{,}229\\)\n\\(100{,}000\\) | \\(9{,}592\\)\n\\(1{,}000{,}000\\) | \\(78{,}498\\)\n\\(1{,}000{,}000{,}000\\) | \\(50{,}847{,}534\\)\n\nThe symbol \\(\\pi\\) here is conventional and does not represent the circle constant."
    },
    {
      "heading": "Asymptotic distribution",
      "text": "The prime number theorem\n\nThe fundamental approximation is the prime number theorem:\n\n\\[\\pi(x)\\sim\\frac{x}{\\log x} \\qquad (x\\to\\infty),\\]\n\nwhere \\(\\log\\) is the natural logarithm. The notation \\(\\sim\\) means that\n\n\\[\\lim_{x\\to\\infty}\\frac{\\pi(x)}{x/\\log x}=1.\\]\n\nThis is a statement about relative error, not exact equality or a bounded absolute difference. The approximation becomes proportionally more accurate as \\(x\\) grows, even though its absolute error need not become smaller.\n\nOne consequence is\n\n\\[\\frac{\\pi(x)}{x}\\longrightarrow0.\\]\n\nThus primes have natural density zero among the positive integers, despite being infinite in number.\n\nThe theorem also motivates the heuristic that an integer near a large value \\(x\\) has a chance of approximately \\(1/\\log x\\) of being prime. This is an approximation to local density, not a claim that primality behaves as a sequence of independent random events. Divisibility and congruence restrictions impose strong arithmetic structure.\n\nThe logarithmic integral\n\nA generally more accurate approximation is the logarithmic integral\n\n\\[\\operatorname{li}(x) =\\operatorname{PV}\\int_0^x\\frac{dt}{\\log t}, \\qquad x>1.\\]\n\nHere \\(\\operatorname{PV}\\) denotes the Cauchy principal value, needed because the integrand is singular at \\(t=1\\). Another common convention uses\n\n\\[\\operatorname{Li}(x)=\\int_2^x\\frac{dt}{\\log t}.\\]\n\nThese functions differ by a constant.\n\nRepeated integration by parts yields the asymptotic expansion\n\n\\[\\operatorname{li}(x)\\sim \\frac{x}{\\log x} \\left( 1+\\frac{1}{\\log x} +\\frac{2!}{(\\log x)^2} +\\frac{3!}{(\\log x)^3} +\\cdots \\right).\\]\n\nThis is an asymptotic series, not a convergent expansion for a fixed finite \\(x\\). Keeping a fixed number of terms gives increasingly refined asymptotic descriptions as \\(x\\) tends to infinity.\n\nThe prime number theorem is equivalently expressed as\n\n\\[\\pi(x)\\sim\\operatorname{li}(x).\\]"
    },
    {
      "heading": "Error terms and the Riemann hypothesis",
      "text": "The difference\n\n\\[E(x)=\\pi(x)-\\operatorname{li}(x)\\]\n\nmeasures the discrepancy between the exact count and its principal smooth approximation.\n\nA classical unconditional estimate states that, for some constant \\(c>0\\),\n\n\\[\\pi(x)=\\operatorname{li}(x) +O\\!\\left(xe^{-c\\sqrt{\\log x}}\\right).\\]\n\nThe Big O notation specifies an eventual bound up to a constant factor. Stronger unconditional estimates are known, but this form already improves substantially on the basic prime number theorem.\n\nThe Riemann hypothesis is equivalent to the estimate\n\n\\[\\pi(x)=\\operatorname{li}(x) +O\\!\\left(\\sqrt{x}\\log x\\right) \\qquad (x\\to\\infty).\\]\n\nThis connects an assertion about zeros of a complex-valued function with a quantitative bound on the irregularity of prime distribution.\n\nAlthough \\(\\operatorname{li}(x)\\) exceeds \\(\\pi(x)\\) throughout very large computed ranges, it is not an upper bound for all \\(x\\). Littlewood proved in 1914 that\n\n\\[\\pi(x)-\\operatorname{li}(x)\\]\n\nchanges sign infinitely often. Agreement over a large finite range therefore does not establish a global inequality."
    },
    {
      "heading": "Weighted counting functions and zeta zeros",
      "text": "In analytic number theory, weighted prime counts are often easier to study than \\(\\pi(x)\\) directly. The two Chebyshev functions are\n\n\\[\\vartheta(x)=\\sum_{p\\le x}\\log p\\]\n\nand\n\n\\[\\psi(x)=\\sum_{p^k\\le x}\\log p,\\]\n\nwhere \\(p\\) runs over primes and \\(k\\) over positive integers. The second function counts prime powers, assigning the weight \\(\\log p\\) to each power of \\(p\\).\n\nThe prime number theorem is equivalent to either of the statements\n\n\\[\\vartheta(x)\\sim x, \\qquad \\psi(x)\\sim x.\\]\n\nSummation by parts gives the exact relation\n\n\\[\\pi(x)= \\frac{\\vartheta(x)}{\\log x} +\\int_2^x\\frac{\\vartheta(t)}{t(\\log t)^2}\\,dt, \\qquad x\\ge2.\\]\n\nAnother useful function is Riemann’s weighted prime-power count:\n\n\\[J(x)=\\sum_{p^k\\le x}\\frac1k.\\]\n\nIt satisfies\n\n\\[J(x)= \\sum_{k\\ge1}\\frac1k\\,\\pi\\!\\left(x^{1/k}\\right).\\]\n\nFor fixed \\(x\\), only finitely many terms are nonzero. Using the Möbius function \\(\\mu\\), this relation can be inverted:\n\n\\[\\pi(x)= \\sum_{k\\ge1}\\frac{\\mu(k)}{k} J\\!\\left(x^{1/k}\\right).\\]\n\nRiemann’s explicit formula expresses a version of \\(J(x)\\) in terms of the logarithmic integral, contributions from the nontrivial zeros of the zeta function, and additional correction terms. The zero contributions explain oscillations around the smooth approximation. At prime powers, an exact explicit formula requires an endpoint convention, commonly assigning the midpoint of a jump.\n\nThese identities provide a bridge between prime counting and complex analysis; their numerical use requires careful truncation and error control."
    },
    {
      "heading": "Exact computation",
      "text": "Sieving\n\nThe most direct systematic method is the sieve of Eratosthenes. It marks composite integers up to a chosen bound and counts the unmarked integers greater than \\(1\\).\n\nA standard implementation uses \\(O(x\\log\\log x)\\) elementary marking operations and storage proportional to \\(x\\). A segmented sieve processes successive intervals, reducing memory requirements while retaining primes up to \\(\\sqrt{x}\\) for marking composites.\n\nSieving is particularly useful when the primes themselves are also needed. If only their number is required, more specialized algorithms can avoid enumerating every prime up to \\(x\\).\n\nLegendre’s formula\n\nLet \\(p_1,p_2,\\ldots\\) be the primes in increasing order, and let \\(\\phi(x,a)\\) count positive integers not exceeding \\(x\\) that are divisible by none of the first \\(a\\) primes. This auxiliary function is not Euler’s totient function.\n\nIt obeys the recurrence\n\n\\[\\phi(x,a)= \\phi(x,a-1) -\\phi\\!\\left(\\frac{x}{p_a},a-1\\right), \\qquad \\phi(x,0)=\\lfloor x\\rfloor.\\]\n\nIf \\(a=\\pi(\\sqrt{x})\\), then\n\n\\[\\pi(x)=\\phi(x,a)+a-1.\\]\n\nThe identity follows because every composite integer at most \\(x\\) has a prime divisor at most \\(\\sqrt{x}\\). After multiples of those primes are removed, the survivors are \\(1\\) and the primes greater than \\(\\sqrt{x}\\).\n\nMeissel–Lehmer methods and later combinatorial algorithms refine this approach by organizing the remaining integers according to their prime factors. Analytic algorithms instead exploit zeta-function identities and controlled approximations. Both approaches can compute exact counts without producing a complete list of the primes being counted."
    },
    {
      "heading": "Historical development",
      "text": "Tables of primes helped reveal that their frequency decreases approximately according to a logarithmic law. In the late eighteenth and early nineteenth centuries, Carl Friedrich Gauss and Adrien-Marie Legendre developed influential approximations to the prime-counting function. Gauss associated prime density with \\(1/\\log x\\), leading to the logarithmic-integral approximation; Legendre proposed approximations of the form \\(x/(\\log x-B)\\).\n\nIn the mid-nineteenth century, Pafnuty Chebyshev established bounds showing that \\(\\pi(x)\\) has the same order of magnitude as \\(x/\\log x\\), without proving that their ratio tends to \\(1\\).\n\nIn 1859, Bernhard Riemann connected prime counting with the analytic properties of the zeta function. Jacques Hadamard and Charles Jean de la Vallée Poussin independently proved the prime number theorem in 1896 by showing, in particular, that the zeta function has no zeros on the line \\(\\operatorname{Re}(s)=1\\).\n\nIn 1949, Atle Selberg and Paul Erdős gave elementary proofs of the prime number theorem. Here “elementary” means that the proofs avoid complex-analytic methods, not that they are simple."
    },
    {
      "heading": "Related counting problems and limitations",
      "text": "If \\(p_n\\) denotes the \\(n\\)-th prime, then\n\n\\[\\pi(p_n)=n, \\qquad p_n=\\min\\{m\\in\\mathbb N:\\pi(m)\\ge n\\}.\\]\n\nThe prime number theorem consequently implies\n\n\\[p_n\\sim n\\log n.\\]\n\nCounts in intervals are expressed by\n\n\\[\\pi(x+h)-\\pi(x),\\]\n\nthe number of primes in \\((x,x+h]\\). The prime number theorem alone does not justify replacing this expression by \\(h/\\log x\\) for every choice of a short interval length \\(h\\). Subtracting two global approximations may leave errors larger than the interval count itself.\n\nA further extension counts primes in residue classes:\n\n\\[\\pi(x;q,a) =\\#\\{p\\le x:p\\equiv a\\pmod q\\}.\\]\n\nFor fixed positive \\(q\\), when \\(a\\) and \\(q\\) are coprime, the prime number theorem for arithmetic progressions states\n\n\\[\\pi(x;q,a)\\sim \\frac{\\operatorname{li}(x)}{\\varphi(q)},\\]\n\nwhere \\(\\varphi\\) is Euler’s totient function. Statements in which \\(q\\) grows with \\(x\\) require additional qualifications and stronger estimates.\n\nGlobal prime counts therefore describe average distribution, but do not by themselves determine individual prime gaps, prime patterns, or distribution in very short intervals."
    }
  ],
  "links": [
    "prime-number",
    "real-number",
    "number-theory",
    "mathematical-analysis",
    "riemann-zeta-function",
    "indicator-function",
    "pi",
    "prime-number-theorem",
    "natural-logarithm",
    "integration-by-parts",
    "asymptotic-expansion",
    "big-o-notation",
    "riemann-hypothesis",
    "mobius-function",
    "complex-analysis",
    "sieve-of-eratosthenes",
    "recurrence-relation",
    "carl-friedrich-gauss",
    "bernhard-riemann",
    "eulers-totient-function"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}