gives the number of primes TemplateBox[{x}, PrimePi] less than or equal to x.

Details and Options

  • PrimePi is also known as prime counting function.
  • Mathematical function, suitable for both symbolic and numerical manipulation.
  • TemplateBox[{x}, PrimePi] counts the prime numbers less than or equal to x.
  • TemplateBox[{x}, PrimePi] has the asymptotic expansion as .
  • The following option can be given:
  • Method Automaticmethod to use
    ProgressReporting $ProgressReportingwhether to report the progress of the computation
  • Possible settings for Method include:
  • "DelegliseRivat"use the DelégliseRivat algorithm
    "Legendre"use the Legendre formula
    "Lehmer"use the Lehmer formula
    "LMO"use the LagariasMillerOdlyzko algorithm
    "Meissel"use the Meissel formula
    "Sieve"use the sieve of Erastosthenes


open allclose all

Basic Examples  (3)

Compute the number of primes up to 15:

Plot the prime counting function:

Find the leading asymptotic term for PrimePi at Infinity:

Scope  (10)

Numerical Evaluation  (5)

PrimePi works over integers:

Rational numbers:

Real numbers:

PrimePi works over large numbers:

PrimePi threads over lists:

Symbolic Manipulation  (5)

The traditional mathematical notation for PrimePi:

Find a solution instance of equalities with PrimePi:

Evaluate an integral:

Recognize the PrimePi function:

Find asymptotic relations:

Verify asymptotic relations:

Options  (6)

Method  (5)

Specify the Legendre method to be used for counting primes [MathWorld]:

Compare the timing:

Specify the Lehmer method to be used for counting primes [MathWorld]:

Compare the timing:

Specify the DelégliseRivat method to be used for counting primes:

Compare the timing:

Specify the Meissel method to be used for counting primes [MathWorld]:

Compare the timing:

Specify the LagariasMillerOdlyzko (LMO) to be used for counting primes:

Compare the timing:

ProgressReporting  (1)

By default, PrimePi does not report progress:

With the setting ProgressReporting->True, PrimePi shows the progress of the computation:

Applications  (22)

Basic Applications  (7)

Visualize the PrimePi function:

Spiral of PrimePi:

Hexagonal prime spiral:

ContourPlot of the PrimePi function:

Plot the difference between RiemannR and the PrimePi function:

Find all prime numbers less than or equal to 100:

Count the number of primes in an interval:

Approximations  (7)

Approximations to PrimePi:

Plot PrimePi compared with estimates:

TemplateBox[{x}, PrimePi] is bounded below by and is bounded above by for greater than 3:

is within of TemplateBox[{x}, PrimePi] for :

TemplateBox[{{TemplateBox[{x}, PrimePi], -, TemplateBox[{x}, LogIntegral]}}, Abs]<(sqrt(x) log(x))/(8 pi) for if the Riemann hypothesis is true:

Count the prime numbers using EulerPhi:

Compute PrimePi based on the HardyWright formula:

Compute PrimePi using Accumulate:

Number Theory  (8)

Find twin primes up to , i.e. pairs of primes of the form :

Plot the sequence of twin primes and PrimePi:

The ^(th) Ramanujan prime is the smallest number such that for all :

Plot the sequence of Ramanujan primes:

Compare the count of Ramanujan primes to PrimePi:

Calculate the primorial up to the ^(th) prime, i.e. a function that multiplies successive primes, similar to the factorial:

Compare the primorial to the factorial up to :

Plot the differences between the factorial and the primorial up to :

Plot the Chebyshev theta function:

Calculate the prime powers up to :

Count all the prime powers up to :

Graph the count of prime powers:

Compare the count of prime powers to PrimePi:

Visualize the second HardyLittlewood conjecture, which states that for :

Plot Brocard's conjecture, which states that if and are consecutive primes greater than 2, then between and there are at least four prime numbers:

Find Goldbach partitions, i.e. pairs of primes (, ) such that :

Graph Goldbach's conjecture/comet:

The only 8 solutions of TemplateBox[{x}, PrimePi]=TemplateBox[{x}, EulerPhi]:

Properties & Relations  (5)

The largest domain of definitions of PrimePi:

PrimePi is asymptotically equivalent to as :

And to LogIntegral:

PrimePi is the inverse of Prime:

Use PrimeQ to count prime numbers:

Mathematical function entity:

An integral representation of PrimePi:

Possible Issues  (1)

Evaluation time increases exponentially:

Neat Examples  (3)

Ulam spiral colored based on the difference in PrimePi values:

Generate a path based on the PrimePi function:

Construct polyhedra using directed graphs generated by primes less than :

Wolfram Research (1991), PrimePi, Wolfram Language function, (updated 2021).


Wolfram Research (1991), PrimePi, Wolfram Language function, (updated 2021).


Wolfram Language. 1991. "PrimePi." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021.


Wolfram Language. (1991). PrimePi. Wolfram Language & System Documentation Center. Retrieved from


@misc{reference.wolfram_2024_primepi, author="Wolfram Research", title="{PrimePi}", year="2021", howpublished="\url{}", note=[Accessed: 22-May-2024 ]}


@online{reference.wolfram_2024_primepi, organization={Wolfram Research}, title={PrimePi}, year={2021}, url={}, note=[Accessed: 22-May-2024 ]}