Next prime after n
Find the smallest prime strictly greater than any whole number: after 10 comes 11, a gap of 1, and after the prime 7 comes 11, not 7 itself. Any value below 2 gives 2. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- N
- 10
- Next prime
- 11
- Gap
- 1
The next-prime problem is simple to state and useful in practice: given an integer n, what is the smallest prime number strictly larger than n?
What “next prime after n” means and where it is used
A prime number is an integer greater than one whose only positive divisors are one and itself. The next prime after n is the least prime p such that p is strictly greater than n. When n is already prime, the answer is the following prime in the sequence, not n itself—so next after seven is eleven, not seven.
When n is less than two, including zero and every negative integer in the safe range, the next prime is always two, because two is the smallest prime. Engineers and students meet this primitive when they need a prime modulus just above a data size, when they walk prime gaps for contest problems, when they build deterministic sample inputs for RSA or Diffie–Hellman demos that must not rely on a local CAS, and when they pad table capacities to a prime to reduce clustering in open-addressing hashes.
How this finder tests candidates and builds the answer
Call the capability with an integer field n. The implementation starts at n plus one and advances through candidates until it finds a prime or exhausts the safe integer range. For candidates up to one million it uses optimized trial division; for larger candidates it applies deterministic Miller–Rabin with the fixed witness set two, three, five, seven, eleven, thirteen, and twenty-three, which is proven sufficient for all integers below far more than two to the fifty-three.
Modular multiplication is written to stay exact for moduli up to Number.MAX_SAFE_INTEGER, so there is no floating-point drift and no reliance on BigInt in the browser path.
What you can do with it
Homework and contest verification
Students and graders can confirm the next prime after any safe integer and inspect the prime gap, without installing a CAS or trusting ad-hoc trial loops.
Prime-sized tables and demo moduli
Scripts that size hash tables or pick teaching moduli just above a data bound can promote n to the next prime with one deterministic item-priced call.
FAQ
What if n is already prime?
You still get the following prime, not n itself.
What happens for negative n or n less than 2?
The next prime is two, the smallest prime. Zero, one, and all safe negative integers behave the same way.
Is the next prime finder free?
The browser tool runs free on your device.
What is the next prime after 10?
It is 11. The result also shows the gap, the difference between the prime found and the number you entered, which here is 1.
How large can the number be?
The next prime must not go beyond 9007199254740991. If it would, you get a clear error instead of an answer that has quietly lost precision.
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/next-prime \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"n":10}'const res = await fetch("https://api.kit.forhosting.com/numth/next-prime", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"n": 10
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/next-prime",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"n": 10
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/next-prime", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"n":10}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"n":10}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/next-prime", 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": 10
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.next_prime",
"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
max_safe_integer | 9007199254740991 |
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
Arithmetic stays inside JavaScript safe integers; if the next prime would exceed that bound, the request fails with invalid_input instead of silently losing precision.
What “next prime after n” means and where it is used
Having a single, pure endpoint that returns n, next_prime, and gap removes ad-hoc loops that copy-paste trial division, disagree on edge cases around one and negatives, or overflow past safe integers without a clear error.
How this finder tests candidates and builds the answer
The response includes the original n, next_prime, and gap equal to next_prime minus n so callers can log prime gaps without a second subtraction. Missing n, non-integers, non-finite values, and integers outside the safe range are rejected as invalid_input and are never charged.
Practical use, limits, pricing, and delivery
Use the browser tool when you want an immediate next prime while studying prime gaps, debugging a hash-table size, or checking a textbook exercise. There is no language model, no external prime service, and no randomness, so the capability is safe to pin in automated tests and offline widgets. Because unit pricing is per item and the work is pure arithmetic, cost stays predictable even when you batch many small n values from a notebook or grading pipeline. Use the API when a script, CI job, or educational platform needs next primes for many values without shipping a primality library. Each successful request is one billable item at $0.002; failed validation does not consume balance. Accepted inputs are integers from negative Number.MAX_SAFE_INTEGER through Number.MAX_SAFE_INTEGER; the result must also be at most Number.MAX_SAFE_INTEGER, otherwise the call errors. Results are fully deterministic: the same n always yields the same next_prime and the same gap. Wire the endpoint into number-theory demos, contest judges that verify “next prime” tasks, or configuration helpers that promote a user-chosen size to the next prime capacity.
Integration use cases
Automated number-theory pipelines
CI jobs and notebooks that enumerate prime gaps or generate prime fixtures can call a pure endpoint instead of embedding Miller–Rabin code in every language.
Technical questions
How do I find the next prime after n with this API?
Send a JSON body with integer field n to the numth.next_prime capability. The response returns next_prime (strictly greater than n) and gap equal to next_prime minus n.
When does the result exceed the safe integer range?
If the smallest prime greater than n would be larger than Number.MAX_SAFE_INTEGER (9007199254740991), the request fails with invalid_input instead of returning an imprecise number.