Prime Puzzles · Number Theory

PrimeCount Calculator: π(x) and the n-th prime

Count the primes up to x, or find the n-th prime, exactly, for any x ≤ 2^63−1 ≈ 9.2 × 10^18 and any n ≤ 216,289,611,853,439,384 ≈ 2.16 × 10^17. The work is done by Kim Walisch's primecount, compiled to run inside your own browser; nothing is sent to the server.

Every value shown as “=” is exact. The approximations Li(x) and R(x) are shown alongside, always marked “≈”, together with their exact error.

Input

digits (commas allowed), + - * ^ ( ) and e-notation; e.g. 10^15, 1e15, 2.5e14, 2^63-1, 3*10^12+7

Examples — hover over an example to see its approximate time on a typical laptop; the last button of each row, marked max, is the largest value this calculator accepts

π(x) ← max p(n) ← max

Session log

QueryResultTime
No runs yet.

What this calculator does

π(x) is the number of primes less than or equal to x; for example π(100) = 25. p(n) is the n-th prime, counting 2 as the first: p(1) = 2, p(1000) = 7919. The two functions are inverse to each other: π(p(n)) = n.

The exact values are computed with primecount, Kim Walisch's implementation of Xavier Gourdon's combinatorial algorithm, compiled to WebAssembly so that it runs entirely inside your browser. This web version uses a single processor core, so the speed depends on that core. As a reference, on an ordinary laptop π(10^16) takes a few seconds, π(10^18) one or two minutes, and the largest accepted input, 2^63−1 ≈ 9.2 × 10^18, a few minutes. During the computation the progress bar follows the steps of Gourdon's algorithm (Σ, Φ₀, A + C, B, D); the step D is the longest one.

Limits. π(x) accepts 0 ≤ x ≤ 2^63−1 = 9,223,372,036,854,775,807. p(n) accepts 1 ≤ n ≤ 216,289,611,853,439,384, which is π(2^63−1): the largest prime this page can return is the last prime below 2^63. These limits come from the 32-bit WebAssembly build of primecount (the native 64-bit program goes up to 10^31). The approximations accept the same ranges.

Long computations. Near the top of the range a single computation can take several minutes, or more on a slower laptop. The work is done by your own computer inside this page, so closing or reloading the page stops the computation. Before starting a long computation, keep the laptop plugged in, turn off battery-saver mode, and set sleep and hibernation to “never” while it runs: a computer that goes to sleep pauses the computation. On phones and tablets keep the screen on and the browser in the foreground, because the system may suspend a browser that runs in the background. You can always stop a computation with the Cancel button.

Need more? If you often need exact values above about 10^17, or you would find useful a faster version of this page that uses all the cores of your computer, please write to cbrfgm@gmail.com; your requests will decide whether such a version is built. For heavy work you can also run the free native primecount program (Windows, Linux and macOS), which uses all the cores of your computer and accepts x up to 10^31.

The n-th prime is found the way primecount does it: the inverse Riemann R function first gives a very close estimate of p(n), then π is computed exactly at that estimate, and finally a sieve walks the short remaining gap to the exact prime.

The approximations Li(x) and R(x)

Together with every exact result the calculator shows two classical approximations and how far each one is from the truth.

Li(x) = ∫ from 2 to x of dt / ln t (offset logarithmic integral) R(x) = Σ μ(k)/k · li(x^(1/k)), k ≥ 1 (Riemann R function)

Li(x) is the approximation behind the Prime Number Theorem, already guessed by Gauss in the 1790s. R(x) is Riemann's refinement (1859), and it is astonishingly accurate. For x = 10^12: π(x) = 37,607,912,018; Li(x) exceeds it by 38,261, while R(x) misses it by only 1,476. Their inverses, Li⁻¹(n) and R⁻¹(n), estimate where the n-th prime lies.

Why they are useful in practice

1. Instant answers. The approximations cost milliseconds at any size, while the exact count grows expensive. Tick “Approximations only” to get them at once, even for the largest inputs.

2. Seeing the Prime Number Theorem at work. The column “exact − approx.” shows, with real numbers, how good each formula is, and how slowly the errors grow compared with x itself.

3. Planning a search. Li(b) − Li(a) estimates how many primes lie in an interval [a, b]. A prime hunter or a puzzler can use it to decide, before launching a long run, whether a search is reasonable.

4. Locating the n-th prime. R⁻¹(n) tells at once, with a tiny relative error, where the n-th prime is.

5. A classical surprise. In every value ever computed, Li(x) is larger than π(x): the difference π(x) − Li(x) is always negative. Yet in 1914 J. E. Littlewood proved that the inequality reverses infinitely often. The first crossing is known to lie beyond 10^19, out of reach of this calculator, and it is linked to the famous Skewes' number.

Why this calculator

To our knowledge, no other free web page computes π(x) and the n-th prime exactly over this range using nothing but a browser. The best-known online tool, The Nth Prime Page at t5k.org, accepts x up to 3 × 10^13 for π(x) and n up to 10^12 for the n-th prime. Other online calculators stop earlier, do not publish their limits, or give only one of the two functions. This page goes up to x = 2^63−1 ≈ 9.2 × 10^18 and n ≈ 2.16 × 10^17, more than 200,000 times further. It also shows what those tools do not: the approximations Li(x) and R(x) and their inverses, each with its exact error, live progress through the steps of the algorithm, and a log of the session. Since every value is computed on your own computer, there is no server queue and no load limit. All the reference values on this page were checked against OEIS A006880 and A006988.

Native programs go further still. primecount itself, the engine of this page, run as a program on your computer, uses all its cores and accepts x up to 10^31. Mathematica and PARI/GP also compute π(x). For values beyond our limits, or for many large computations, those are the right tools; this page is meant for exact answers with nothing to install.

But... as with any publicly available tool, occasional errors may arise; notifications via email regarding any issues will be sincerely appreciated.

Credits

primecount and primesieve by Kim Walisch — github.com/kimwalisch/primecount. primecount has been used to compute several world records, among them π(10^27) and the 10^24-th prime. This page uses primecount 9.1.

Prime counting algorithms by A.-M. Legendre, E. D. F. Meissel, D. H. Lehmer, J. C. Lagarias, V. S. Miller & A. M. Odlyzko, M. Deléglise & J. Rivat, and Xavier Gourdon (the algorithm used here).

primecount and primesieve license (BSD 2-Clause)
BSD 2-Clause License

Copyright (c) 2013 - 2026, Kim Walisch.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.