Prime Gap Calculator

Find the next and previous primes around a number and the distance to the next one, with a guarded search.

Math and PercentUpdated 2026-11-182 min read
Prime Gap Calculator
A whole number from 2 to 10,000,000.
Next prime above n
...
Gap to next prime...
Previous prime below n...

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

  1. Enter a whole number n from 2 to ten million.
  2. Read the next prime above n in the main result box.
  3. Check the gap beside it, which is the next prime minus n.
  4. Read the previous prime below n for context.
  5. The default n = 100 reproduces the worked example.
  6. Try n = 113 to see a large gap of 14.

Formula

gap(n) = p - n, where p is the smallest prime greater than n
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.

  1. Test 101 for primality. It has no divisor other than 1 and itself, so it is prime.
  2. The next prime above 100 is therefore 101.
  3. Compute the gap: 101 - 100 = 1.
  4. Search downward from 99. It is divisible by 9 and 11, so it is not prime.
  5. Continue to 97, which is prime.
  6. The tool reports next prime 101, gap 1, and previous prime 97.
  7. For 113 the next prime is 127, giving a gap of 14.

Reference table

Primes and gaps around sample values
nPrevious primeNext primeGap
2n/a31
107111
8983978
97891014
100971011
11310912714

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.

Sources

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.