Prime Gap Calculator
Find the next and previous primes around a number and the distance to the next one, with a guarded search.
What it does
Prime gaps are the distances between consecutive primes. The sequence begins with gaps of one, two, two, four and so on, and it is a central topic in number theory because the gaps reveal how primes thin out as numbers grow. The average gap near n is roughly the natural logarithm of n.
This page answers a slightly more general question. Given any whole number n, it finds the first prime strictly greater than n and reports the distance from n to that prime. When n is itself prime, the result is exactly the ordinary prime gap after n.
The search uses a straightforward primality test on each candidate. It walks upward from n plus one until a prime is found, and downward from n minus one for the previous prime. Both loops carry a guard, so even a hostile input cannot keep the page busy without end.
Alongside the next prime the tool shows the gap and the previous prime. That pair brackets your input and makes the local density of primes easy to see at a glance.
The upper limit is ten million. At that size the primality test remains fast in a browser, and the search typically needs only a handful of candidates before it finds a prime.
How to use
- Enter a whole number n from 2 to ten million.
- Read the next prime above n in the main result box.
- Check the gap beside it, which is the next prime minus n.
- Read the previous prime below n for context.
- The default n = 100 reproduces the worked example.
- Try n = 113 to see a large gap of 14.
Formula
- n
- The starting whole number.
- p
- The smallest prime strictly greater than n.
- gap(n)
- The distance from n up to that prime.
If n is prime the result matches the standard prime gap after that prime. If n is composite the gap measures the distance to the next prime rather than a gap between two consecutive primes. The search is limited to a fixed number of candidates, which is far more than needed anywhere in the allowed range.
Worked example
Example: n = 100
The page opens with n = 100.
- Test 101 for primality. It has no divisor other than 1 and itself, so it is prime.
- The next prime above 100 is therefore 101.
- Compute the gap: 101 - 100 = 1.
- Search downward from 99. It is divisible by 9 and 11, so it is not prime.
- Continue to 97, which is prime.
- The tool reports next prime 101, gap 1, and previous prime 97.
- For 113 the next prime is 127, giving a gap of 14.
Reference table
| n | Previous prime | Next prime | Gap |
|---|---|---|---|
| 2 | n/a | 3 | 1 |
| 10 | 7 | 11 | 1 |
| 89 | 83 | 97 | 8 |
| 97 | 89 | 101 | 4 |
| 100 | 97 | 101 | 1 |
| 113 | 109 | 127 | 14 |
Limits
- The next prime is strictly greater than n, never n itself.
- Inputs above ten million are rejected.
- The previous prime is shown as not applicable for n = 2, since 2 is the first prime.
- The search guard caps how many candidates are tried in either direction.
Common mistakes
- Counting n itself as the next prime when n is prime.
- Taking the gap to the previous prime instead of the next one.
- Assuming gaps grow steadily with n.
- Forgetting that 2 has no earlier prime.
- Confusing the gap value with the number of composite integers in between.
Try it yourself
Quick checks. Answers sit tucked under each question.
What is the gap after 113?
The next prime is 127, so the gap is 127 - 113 = 14.
What is the gap after 97?
The next prime is 101, so the gap is 4 and the previous prime is 89.
What is the next prime above 100?
It is 101, giving a gap of 1 and a previous prime of 97.
Questions
What is a prime gap?
The difference between consecutive primes, or here the distance from n to the next prime.
How large are gaps on average?
Near n the average gap is about the natural logarithm of n, though individual gaps vary widely.
Can the gap be one?
Yes, for twin primes and for any even n immediately below an odd prime, as with 100 and 101.
What is the previous prime for n = 2?
There is none, since 2 is the smallest prime, so the tool reports not applicable.
How does the search stay safe?
Both upward and downward loops have a fixed candidate guard.
What is the largest input?
Ten million, with a clear error for larger values.