Number of primes up to a given bound

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.




numlib::pi(x) returns the number of primes not exceeding x.

If the argument x is a real number (an integer, rational, or floating-point number), then the number of primes below x is returned. If x is a complex number, numlib::pi stops with an error. For every other kind of arithmetical expression x, an unevaluated call is returned.

numlib::pi becomes slightly faster if the internal prime number table is large. ifactor(PrimeLimit) displays the limit of the internal prime number table; it can be set by the user via the command line flag -L.

Internally, a fast kernel function with constant memory consumption is used for the computation.


Example 1

There are two primes less or equal 3:


Example 2

Also larger inputs can be handled fast:


Example 3

Floating point arguments are allowed, too.




An arithmetical expression

Return Values

Non-negative integer or an unevaluated call to numlib::pi


A Lehmer-type algorithm is used, with no precomputed sieve array and no remember tables. In contrast to the algorithm in “Computing π: The Meissel-Lehmer method”, this means constant memory consumption, at the price of slowness.


[1] Lagarias, J.C., V.S. Miller, and A.M. Odlyzko. “Computing π: The Meissel-Lehmer method”, Math. Comp., Vol. 44, No. 170 (1985), pp. 537-560

See Also

MuPAD Functions