Ask Your Question

Revision history [back]

The prime_range function accepts only numbers as arguments, not symbolic expressions.

Depending on your intended use of these prime ranges, that might be enough.

For instance, to count the number of primes in each such range:

for n in range(1, 21):
    a = n^2 + 1
    b = (n + 1)^2 + 1
    nprimes = sum(1 for p in prime_range(a, b))
    print(f"n = {n}: {nprimes} primes")

The output is:

n = 1: 2 primes
n = 2: 2 primes
n = 3: 2 primes
n = 4: 3 primes
n = 5: 2 primes
n = 6: 4 primes
n = 7: 3 primes
n = 8: 4 primes
n = 9: 3 primes
n = 10: 5 primes
n = 11: 4 primes
n = 12: 5 primes
n = 13: 5 primes
n = 14: 4 primes
n = 15: 6 primes
n = 16: 7 primes
n = 17: 5 primes
n = 18: 6 primes
n = 19: 6 primes
n = 20: 7 primes

The prime_range function accepts only numbers as arguments, not symbolic expressions.

Depending on your intended use of these prime ranges, that might be enough.

For instance, to count the number of primes in each such range:

for n in range(1, 21):
    a = n^2 + 1
    b = (n + 1)^2 + 1
    nprimes = sum(1 for p in prime_range(a, b))
    print(f"n = {n}: {nprimes} primes")

The output is:

n = 1: 2 primes
n = 2: 2 primes
n = 3: 2 primes
n = 4: 3 primes
n = 5: 2 primes
n = 6: 4 primes
n = 7: 3 primes
n = 8: 4 primes
n = 9: 3 primes
n = 10: 5 primes
n = 11: 4 primes
n = 12: 5 primes
n = 13: 5 primes
n = 14: 4 primes
n = 15: 6 primes
n = 16: 7 primes
...
n = 17: 5 primes
n = 18: 6 primes
n = 19: 6 primes
n = 20: 7 primes

The prime counting function prime_pi gives another way to get that count:

def primes_between_squares(n):
    r"""
    Return the number of squares between n^2 and (n+1)^2.
    """
    return prime_pi((n+1)^2) - prime_pi(n^2)

for n in range(1, 21):
    print(f"n = {n}: {primes_between_squares(n)} primes")

(same output).

The prime_range function accepts only numbers as arguments, not symbolic expressions.

Depending on your intended use of these prime ranges, that might be enough.

For instance, to count the number of primes in each such range:

for n in range(1, 21):
    a = n^2 + 1
    b = (n + 1)^2 + 1
    nprimes = sum(1 for p in prime_range(a, b))
    print(f"n = {n}: {nprimes} primes")

The output is:

n = 1: 2 primes
n = 2: 2 primes
n = 3: 2 primes
n = 4: 3 primes
n = 5: 2 primes
...
n = 16: 7 primes
n = 17: 5 primes
n = 18: 6 primes
n = 19: 6 primes
n = 20: 7 primes

The prime counting function prime_pi gives another way to get that count:

def primes_between_squares(n):
    r"""
    Return the number of squares between n^2 and (n+1)^2.
    """
    return prime_pi((n+1)^2) - prime_pi(n^2)

for n in range(1, 21):
    print(f"n = {n}: {primes_between_squares(n)} primes")

(same output).