Skip to content

Prime Gap Calculator

    A prime gap is the difference between two consecutive prime numbers. Enter one number to find the gap around it, check whether two primes are truly consecutive, or examine every prime gap inside a selected range.

    Prime Gap Calculator

    The calculator verifies primality and checks for primes between the endpoints. It does not label an ordinary difference between two nonconsecutive primes as a prime gap.

    Whole numbers from 2 through 18,446,744,073,709,551,615 are accepted.

    The range may span up to 1,000,000 integers, with an upper endpoint no greater than 1,000,000,000.

    What the Prime Gap Calculator Measures

    Let pn be a prime number and let pn+1 be the next prime after it. The prime gap is:

    gn = pn+1 − pn

    The word next matters. Two numbers can both be prime and still fail to form one prime gap. If another prime lies between them, their difference is only a difference between primes.

    Valid Consecutive Pair

    23 and 29 are prime, and no prime lies between them.

    Prime gap: 29 − 23 = 6

    Not a Consecutive Pair

    11 and 17 are prime, but 13 lies between them.

    Their difference is 6, but they do not define one prime gap.

    Prime Gap and Numbers Between the Primes

    A gap value is not the same as the count of integers between the two endpoint primes. For consecutive primes p and q:

    Prime gap = q − p
    Integers between = q − p − 1

    For 23 and 29, the prime gap is 6. The five integers between them are 24, 25, 26, 27, and 28.

    Do not subtract one when reporting the standard prime gap. The subtraction by one is used only when counting the integers located strictly between the two primes.

    Early Prime Gaps

    Examples of consecutive primes and their prime gaps
    Lower PrimeNext PrimeCalculationPrime GapIntegers Between
    233 − 210
    355 − 321
    71111 − 743
    232929 − 2365
    899797 − 8987

    Why Almost Every Prime Gap Is Even

    The first prime gap, between 2 and 3, equals 1. It is the only odd prime gap.

    Every prime greater than 2 is odd. Subtracting one odd number from another gives an even number, so every prime gap after 2 and 3 is even. This explains why the sequence begins:

    1, 2, 2, 4, 2, 4, 2, 4, 6, 2, 6, 4…

    The values do not rise in a steady pattern. A larger gap can be followed by a smaller one, and the same gap can appear many times.

    Twin Primes and Other Prime Differences

    A prime gap of 2 creates a pair of twin primes, such as 11 and 13 or 29 and 31. These pairs are automatically consecutive because the only integer between two odd numbers that differ by 2 is even.

    For larger differences, checking the interval is necessary. The primes 5 and 11 differ by 6, but 7 lies between them. By contrast, 23 and 29 are consecutive and produce an actual gap of 6.

    When only primality needs to be tested, the Prime Number Checker can verify an individual value. The calculator on this page adds the second step: it confirms that no other prime occurs between the endpoints.

    Prime Gaps Inside a Range

    The range mode finds every prime in the selected interval, pairs each prime with the next one inside that interval, and calculates their differences. It also reports the largest gap found there, the average gap, and the most common gap value.

    Largest in the selected range does not automatically mean a record across all smaller primes. A record-setting gap must exceed every earlier prime gap, which requires a wider comparison than one local range.

    Range boundary rule: only pairs whose lower and upper primes both lie inside the chosen interval are included. A gap crossing the start or end boundary is not counted.

    Average Gap Near a Large Number

    Prime numbers become less dense as values grow. Near a number x, the average spacing between primes is roughly related to the natural logarithm ln(x).

    Approximate average spacing near x ≈ ln(x)

    This is a statistical comparison, not a prediction for the next pair. An individual gap can be smaller or much larger than ln(x). The calculator therefore reports the exact gap first. Any logarithmic comparison should remain secondary.

    How the Calculation Works

    Gap Around One Number

    If the entry is prime, the calculator uses it as the lower endpoint and finds the next prime. If the entry is composite, it finds the nearest prime below and the nearest prime above, placing the entered number inside that prime gap.

    Checking Two Numbers

    Both entries are tested for primality. The calculator then finds the next prime after the lower value. The pair is consecutive only when that next prime equals the upper value.

    Scanning a Range

    The range tool uses a segmented sieve. It first generates the small primes needed to test the interval, then marks composite numbers within the selected segment. This avoids building an array for every integer from 2 to the upper endpoint.

    Input Limits and Exact Integer Handling

    The single-number and two-number modes use integer arithmetic and a deterministic primality test for values within the displayed 64-bit range. Decimal values, negative values, commas, spaces, and scientific notation are rejected.

    The range mode has a smaller limit because it must inspect many values at once. Its cap keeps the page responsive in a browser and prevents a very wide request from freezing the tab.

    What a Prime Gap Does Not Predict

    • It does not determine the size of the following gap.
    • It does not show when the same gap will appear again.
    • It does not prove that a gap value occurs infinitely often.
    • It does not make two primes consecutive merely because their difference is even.
    • It does not turn the largest result in one range into a global record.

    Questions About Prime Gaps

    Can a prime gap be odd?

    Yes, but only once. The gap between 2 and 3 is 1. Every later prime gap is even because both endpoint primes are odd.

    Are any two prime numbers a prime gap pair?

    No. They must be consecutive primes. If another prime appears between them, their difference is not one prime gap.

    Is a gap of 6 the same as six composite numbers between the primes?

    No. A gap of 6 means the endpoint primes differ by 6. There are five integers between them, and all five are composite when the endpoints are consecutive primes.

    Can the calculator predict the next prime gap?

    No. It finds primes and measures an existing interval. Prime gaps do not follow a simple repeating rule that determines the next value.

    Does every even number occur as a prime gap?

    Many even values occur as gaps between consecutive primes. The claim that each positive even number occurs infinitely often is related to Polignac’s conjecture and remains unproved.

    Why are 5 and 11 not a prime gap of 6?

    Both endpoints are prime, but 7 lies between them. The consecutive gaps are 7 − 5 = 2 and 11 − 7 = 4.

    📌

    Complete guide: Prime Number Tools