PRIMERANGE

This function returns all prime numbers in a half-open interval. With one bound, it returns primes in [2, a); with two bounds, it returns primes in [a, b).

The output is the ordered set of integers p such that each p is prime and satisfies the interval constraints.

The returned values are arranged as a single-column 2D array for Excel range output.

Excel Usage

=PRIMERANGE(a, b)
  • a (int, required): Lower bound or upper bound when b is omitted.
  • b (int, optional, default: null): Exclusive upper bound when provided.

Returns (list[list]): Single-column 2D array of prime numbers in the requested range.

Example 1: Primes less than twenty

Inputs:

a b
20

Excel formula:

=PRIMERANGE(20, )

Expected output:

Result
2
3
5
7
11
13
17
19
Example 2: Primes in a bounded interval

Inputs:

a b
7 30

Excel formula:

=PRIMERANGE(7, 30)

Expected output:

Result
7
11
13
17
19
23
29
Example 3: Interval with few primes

Inputs:

a b
30 40

Excel formula:

=PRIMERANGE(30, 40)

Expected output:

Result
31
37
Example 4: Small upper bound interval

Inputs:

a b
5

Excel formula:

=PRIMERANGE(5, )

Expected output:

Result
2
3

Python Code

from sympy import primerange as sympy_primerange

def primerange(a, b=None):
    """
    Generate all prime numbers in a specified interval.

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

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

    Args:
        a (int): Lower bound or upper bound when b is omitted.
        b (int, optional): Exclusive upper bound when provided. Default is None.

    Returns:
        list[list]: Single-column 2D array of prime numbers in the requested range.
    """
    try:
        if b is None:
            values = list(sympy_primerange(a))
        else:
            values = list(sympy_primerange(a, b))

        return [[int(v)] for v in values]
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Lower bound or upper bound when b is omitted.
Exclusive upper bound when provided.