Prime Gap Calculator

Enter a number to see its surrounding primes and the prime gap.

Result

127
113
14

How it is calculated

For a whole number n, the tool looks for the nearest prime strictly below it, p, and the nearest prime strictly above it, q, then reports gap = q − p. Each candidate is tested by trial division: even numbers other than 2 are rejected, and odd divisors are tried up to the square root of the candidate. The search moves down from n − 1 and up from n + 1 one step at a time. Accepted inputs are whole numbers from 3 to 1,000,000,000; decimals, numbers below 3 and numbers above the limit give no result. If n is itself prime, it is skipped, so the gap spans across it and equals the sum of two consecutive prime gaps.

Worked example

Take n = 114. Going down: 113 is prime (not divisible by 2, 3, 5 or 7, and 11 is already above its square root, which is about 10.6), so the previous prime is 113. Going up: 115 = 5 × 23, 116 and 118 are even, 117 = 9 × 13, 119 = 7 × 17, 120 is even, 121 = 11 × 11, 122 and 124 and 126 are even, 123 = 3 × 41, 125 = 5³, and 127 is prime. The next prime is 127, so the gap is 127 − 113 = 14.

Try this example →

Common mistakes

  • Entering a prime and expecting its own gap to the next prime. A prime input is skipped, so 113 returns 109 and 127 with a gap of 18, not the 14 between 113 and 127.
  • Reading the gap as the count of composite numbers in between. The gap is the difference of the two primes, so 113 and 127 have 13 numbers between them, not 14.
  • Typing a number with decimals, a negative or 2 and expecting an answer. Only whole numbers from 3 up to one billion are accepted.
  • Assuming the result is the next prime after a prime-looking number. Numbers such as 91 look prime but are not, so the tool checks them properly and 91 is not treated as a neighbour prime.

About this calculator

A prime gap is simply the distance between one prime number and the next one along the number line. This calculator finds the largest prime below your chosen number and the smallest prime above it, then reports the gap between those two neighbors, letting you see how prime numbers thin out or cluster around any point you pick.

Prime gaps are famously irregular: the Prime Number Theorem tells us that on average, gaps near a number n grow roughly in proportion to the natural logarithm of n, so primes get sparser as numbers get bigger. But locally, gaps swing wildly — twin primes sit only 2 apart, while long stretches of consecutive composite numbers can create gaps of thousands. Whether every gap of size 2 recurs forever is the famous unsolved twin prime conjecture, and the general behavior of gaps is a major topic in analytic number theory and computational searches for record-sized gaps.

Students use tools like this to build intuition about prime distribution, and programmers and cryptographers use similar logic when picking candidate primes for keys, where knowing the neighborhood of primes around a starting point matters.

Was this helpful?

You can pick up to 3 reactions.

Comments

  • Be the first to comment.

Popular calculators

All Calculators