Prime Counting Calculator
Count the primes up to a limit with a sieve, and see the largest one and the share of the range they occupy.
What it does
The prime counting function, written pi(n), answers a simple question: how many primes are there from 2 up to n? It is not related to the circle constant. The symbol was chosen because the prime number theorem connects the count to the logarithmic integral, and early notation used the same letter for both ideas.
For small limits the answer is easy to find by listing primes. Up to 10 there are four, up to 100 there are twenty five, and up to 1000 there are 168. Beyond that, listing by hand becomes tedious, and a systematic method is better.
The sieve of Eratosthenes is that method. Start with every integer from 2 to n marked as a candidate. Walk upward, and whenever a number is still marked, cross off all of its multiples. What remains at the end is exactly the set of primes, so counting them gives pi(n) directly.
This page runs the sieve in the browser, counts the survivors, reports the largest one at or below the limit, and converts the count into a percentage of the whole range. The sieve uses a compact byte array, which keeps ten million as a practical upper bound.
The prime number theorem says that pi(n) grows roughly like n divided by the natural log of n. The percentage therefore falls as the limit rises. At 100 a quarter of the integers are prime, while at 1000 it is under a fifth.
How to use
- Enter a whole limit n from 2 to ten million.
- Read pi(n) as the main result.
- Check the largest prime reported beside it.
- Read the share to see how dense primes are in that range.
- Use the default n = 100 to reproduce the worked example.
- Try n = 1000 to see the share drop below twenty percent.
Formula
- n
- The upper limit, included in the count if it is prime.
- pi(n)
- The number of primes at or below n.
- share
- pi(n) divided by n, shown as a percentage with two decimals.
The sieve marks 0 and 1 as non prime, then for each remaining k up to the square root of n crosses off k squared, k squared plus k, and so on. Starting at k squared avoids repeating earlier work. The largest prime is the final unmarked entry, and the share is simply the count over the limit multiplied by 100.
Worked example
Example: limit 100
The tool opens with n = 100, the default value.
- Create candidates 2 through 100 and mark 0 and 1 as non prime.
- Cross off multiples of 2 starting at 4, then multiples of 3 starting at 9.
- Continue with 5, 7 and then primes up to the square root of 100, which is 10.
- Count the entries still marked: there are 25 primes.
- The largest surviving entry is 97.
- Compute the share as 25 / 100 = 25.00 percent.
- The tool reports 25 primes, largest 97, and a share of 25.00 %.
Reference table
| n | pi(n) | Largest prime | Share |
|---|---|---|---|
| 10 | 4 | 7 | 40.00 % |
| 100 | 25 | 97 | 25.00 % |
| 1000 | 168 | 997 | 16.80 % |
| 10000 | 1229 | 9973 | 12.29 % |
| 100000 | 9592 | 99991 | 9.59 % |
Limits
- The count is inclusive, so a prime n is counted.
- Limits above ten million are rejected to protect browser memory.
- The tool does not estimate pi(n) for huge values or use the logarithmic integral.
- The share is a simple ratio, not an asymptotic approximation.
Common mistakes
- Excluding n when n is itself prime.
- Treating 1 as a prime and adding it to the count.
- Crossing off multiples starting at k instead of k squared, which wastes work but does not change the answer.
- Confusing pi(n) with the nth prime.
- Rounding the share to a whole percent before comparing ranges.
Try it yourself
Quick checks. Answers sit tucked under each question.
What is pi(10)?
The primes 2, 3, 5 and 7 give pi(10) = 4, with largest prime 7 and a share of 40.00 %.
How many primes are there up to 1000?
pi(1000) = 168, the largest is 997, and the share is 16.80 %.
Why is the share falling as n grows?
Primes thin out on average, roughly in line with one over the natural log of n, as the prime number theorem states.
Questions
Is pi(n) the circle constant?
No. Here it is the prime counting function, a name that predates the common notation for the circle constant.
What is the largest input?
Ten million. Above that the byte array would use too much memory in a browser.
Does the sieve need every prime up to n?
No. Marking multiples for primes up to the square root of n is enough to remove every composite.
How fast does pi(n) grow?
Roughly like n divided by the natural log of n, according to the prime number theorem.
What is the largest prime under 100?
97. The next candidate, 99, is divisible by 9 and 100 is even.
Can the count include the limit itself?
Yes. If n is prime it is part of the count, which is why pi(10) is four rather than three.