NEXTPRIME

This function finds the next prime after a given integer, or more generally the i-th prime greater than that integer.

If p_k denotes the ordered sequence of primes, the function returns p_{m+i} where p_m \le n < p_{m+1}.

This is useful for stepping through the prime sequence from an arbitrary starting point.

Excel Usage

=NEXTPRIME(n, ith)
  • n (int, required): Starting integer.
  • ith (int, optional, default: 1): Positive index offset in the sequence of primes.

Returns (int): The ith prime strictly greater than n.

Example 1: Next prime after ten

Inputs:

n ith
10 1

Excel formula:

=NEXTPRIME(10, 1)

Expected output:

11

Example 2: Second prime after two

Inputs:

n ith
2 2

Excel formula:

=NEXTPRIME(2, 2)

Expected output:

5

Example 3: Next prime after an existing prime

Inputs:

n ith
13 1

Excel formula:

=NEXTPRIME(13, 1)

Expected output:

17

Example 4: Next prime after negative integer

Inputs:

n ith
-20 1

Excel formula:

=NEXTPRIME(-20, 1)

Expected output:

2

Python Code

from sympy import nextprime as sympy_nextprime

def nextprime(n, ith=1):
    """
    Return the ith prime number greater than a given integer.

    See: https://docs.sympy.org/latest/modules/ntheory.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        n (int): Starting integer.
        ith (int, optional): Positive index offset in the sequence of primes. Default is 1.

    Returns:
        int: The ith prime strictly greater than n.
    """
    try:
        result = sympy_nextprime(n, ith=ith)
        return int(result)
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Starting integer.
Positive index offset in the sequence of primes.