The task is to print all prime numbers within a given interval by taking a starting and ending number. A prime number is a number greater than 1 that has no divisors other than 1 and itself.
Example: In the range [2, 7], the prime numbers are 2, 3, 5, and 7. Different approaches can be used to find prime numbers in an interval.
Using sieve of Eratosthenes
Sieve of Eratosthenes finds all prime numbers up to a given limit by marking the multiples of each prime number as non-prime. It is efficient for finding multiple prime numbers within a large interval.
x, y = 2, 7
primes = [True] * (y + 1)
primes[0], primes[1] = False, False
for i in range(2, int(y ** 0.5) + 1):
if primes[i]:
for j in range(i * i, y + 1, i):
primes[j] = False
result = [i for i in range(x, y + 1) if primes[i]]
print(result if result else "No")
Output
[2, 3, 5, 7]
Explanation:
- primes creates a Boolean list where each index represents a number.
- False is assigned to 0 and 1 because they are not prime.
- The loop checks numbers up to ây.
- Multiples of each prime are marked as False.
- The remaining True values in the given interval represent prime numbers.
Using trial division
Trial Division checks each number in the interval for divisibility. Each number is tested from 2 to its square root.
x, y = 2, 7
res = []
for n in range(x, y + 1):
if n <= 1:
continue
is_prime = True # assume n is prime
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
is_prime = False # n is not prime
break
if is_prime:
res.append(n) # add prime number
print(res if res else "No")
Output
[2, 3, 5, 7]
Explanation:
- Each number from x to y is checked.
- Numbers less than or equal to 1 are skipped.
- Each remaining number is tested for divisibility up to ân.
- If a divisor is found, the number is not prime.
- Numbers with no divisors are added to result.
Using sympy library
SymPy provides the primerange() function to generate prime numbers within a specified range.
from sympy import primerange
x, y = 2, 7
primes = list(primerange(x, y + 1))
print(primes if primes else "No")
Output
[2, 3, 5, 7]
Explanation:
- primerange(x, y + 1) generates prime numbers from x through y.
- list() converts the generated values into a list.
- The result is stored in primes.
Note: SymPy is an external Python library, not part of the standard Python installation, therefore it must be install first using command - "pip install sympy"
Using naive approach
The naive approach checks every number by testing divisibility from 2 to n // 2. It is less efficient than trial division and is mainly suitable for small intervals.
x, y = 2, 7
res = []
for i in range(x, y + 1):
if i <= 1:
continue
for j in range(2, i // 2 + 1):
if i % j == 0:
break
else:
res.append(i)
print(res if res else "No")
Output
[2, 3, 5, 7]
Explanation:
- Each number from x to y is checked.
- Numbers less than or equal to 1 are skipped.
- Each remaining number is divided by values from 2 to n // 2.
- If a divisor is found, the loop stops.
- If no divisor is found, the number is added to result.