Prime counting function π(n)
Counts how many primes are less than or equal to n: π(10) is 4, because the primes are 2, 3, 5 and 7. Each answer also gives the largest prime at or below your number, for any bound up to five million. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 100
- Pi
- 25
- Quantity
- 25
- Largest prime
- 97
The prime counting function π(n) answers a simple question with deep number-theory roots: how many prime numbers are less than or equal to n? From contest warm-ups through analytic estimates such as the prime number theorem, π(n) is the standard tally of primes in an initial interval. This tool evaluates π(n) deterministically with a Sieve of Eratosthenes, then returns both the count and the largest prime found at or below your bound.
What π(n) means and why a sieve is the right engine
By definition π(n) equals the number of primes p such that p is at most n. For n equals ten the primes are two, three, five, and seven, so π(10) is four. For n equals one hundred the count is twenty-five and the largest prime is ninety-seven.
Listing candidates one by one with trial division works for tiny n but scales poorly when you only need the count: each candidate costs a root-bounded factor search, and you still must scan every integer up to n. The classic complexity is on the order of n log log n arithmetic steps with linear memory in n. That is exactly the profile of a free parse capability: pure CPU, no I/O, and a result that never depends on a model, a clock, or external state.
Where prime counts show up in real work
Contest programmers use π(n) as a building block for sieve tables, segmented primes, and problems that ask how many primes fall in an initial range before applying harder filters. Teachers assign π(n) exercises and need a deterministic oracle that also reports the largest prime so students can cross-check their hand sieves. Data and systems engineers sometimes need prime-density estimates when sizing hash tables, choosing moduli near a bound, or validating that a generated prime list has the expected cardinality.
If you later need the full list of primes, primality of a single large integer, or factorization, treat those as separate search intents rather than overloading this count-focused landing. π(n) stays the right page when the question is simply how many primes sit at or below n.
What you can do with it
Prime-density sanity checks
Confirm that a generated prime list up to n has the expected cardinality before using it in hashes, moduli, or teaching demos.
FAQ
What is the prime counting function π(n)?
π(n) is the number of prime numbers less than or equal to n. For example π(10)=4 and π(100)=25. This tool computes that count by sieving.
Does this list every prime up to n?
No. It returns the count (pi/count) and the largest prime ≤ n. Listing every prime is a different task and a different search intent.
What does it cost?
The browser tool is free and runs locally.
How many primes are there up to 100?
There are 25 primes up to 100, so π(100) is 25, and the largest of them is 97. Up to 1000 the count rises to 168.
What is the largest number I can enter?
Any whole number from 2 up to five million. Numbers below 2 are rejected, because there are no primes up to 1 and a plain zero would be easy to misread.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/numth/prime-counting \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":100}'const res = await fetch("https://api.kit.forhosting.com/numth/prime-counting", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 100
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/prime-counting",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 100
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/prime-counting", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":100}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":100}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/prime-counting", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"n": 100
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.prime_counting",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
min_n | 2 |
max_n | 5000000 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
What π(n) means and why a sieve is the right engine
The Sieve of Eratosthenes flips the work: allocate a boolean (or byte) table of size n plus one, mark multiples of each prime starting from its square, then walk the table once counting unmarked entries from two through n. This endpoint implements that sieve, records the last unmarked index as largest_prime, and exposes π(n) under both pi and count so SDKs and classroom notes can use either name without ambiguity.
Input contract, limits, and the shape of a successful reply
Non-integers, empty fields, and values above five million also fail validation and are not charged. Spot checks you can trust: π(2) is one with largest prime two; π(10) is four with largest prime seven; π(100) is twenty-five with largest prime ninety-seven; π(1000) is one hundred sixty-eight. Send a single integer field n (aliases number, value, and limit are accepted). Values below two are rejected with invalid_input because there are no primes less than or equal to one, and this product treats that bound as a clear client error rather than returning a zero that is easy to misread in forms. On success the response includes the normalized n, pi and count with the same integer value, and largest_prime equal to the greatest prime at or below n. API pricing is the published base of $0.002 per successful item; failed validation does not consume balance.
Where prime counts show up in real work
Because the capability is a pure parse engine, the same n always yields the same JSON with no network dependency and no stored state.
Integration use cases
Contest and homework checks
Verify π(n) for olympiad warm-ups and sieve worksheets, with largest_prime returned so hand-marked tables can be audited against the count.
Pipeline-friendly number theory
Call the same deterministic contract from scripts and CI so prime-count fixtures never depend on a spreadsheet or a casual web form.
Technical questions
What values of n are accepted?
Integers from 2 through 5,000,000. Values below 2, above the max, decimals, and non-numeric strings fail with invalid_input.
Why is n below 2 an error?
There are no primes ≤ 1. This endpoint requires n ≥ 2 so forms and APIs surface a clear bound error instead of a silent zero that is easy to misread.