Open In App

Python Program to Print all Prime numbers in an Interval

Last Updated : 21 Oct, 2025
Comments
Improve
Suggest changes
116 Likes
Like
Report

The Task is to print all prime numbers in a given range by taking a starting and ending number as input. A prime number is a number greater than 1 that has no divisors other than 1 and itself.

For example, in the range [2, 7], prime numbers are 2, 3, 5 and 7. Let's look at different ways

Using sieve of eratosthenes

Sieve of Eratosthenes algorithms finds all prime numbers in a given range, especially for large intervals. It works by creating a boolean array where each index represents a number and values marked as True for prime and False for non-prime.

Python
x, y = 2, 7 
primes = [True] * (y + 1)

# 0 and 1 are not prime
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

res = [i for i in range(x, y + 1) if primes[i]]
print(res if res else "No")

Output
[2, 3, 5, 7]

Explanation: It creates a boolean list assuming all numbers are prime. Starting from 2 to √y, it marks multiples of each prime as False i.e., non-prime. After this, indices marked as True represent prime numbers in the range.

Using trial division

Trial Division Method involves checking each number in the interval individually for primality. For each number, we test divisibility from 2 to √n. If a number is divisible by any value in this range, it is not prime.

Python
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: For n in the range, it checks if n is greater than 1. It then assumes n is prime and checks divisibility from 2 to √n. If n is divisible by any number in this range, it is marked as non-prime. If no divisors are found, n is added to the result list.

Using sympy library

SymPy Library provides a built-in function called primerange(x, y) to generate prime numbers in a given range efficiently. Internally, SymPy uses optimized algorithms, including the sieve method.

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"

Python
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) returns an iterator of prime numbers from x to y. The result is converted into a list and stored in primes.

Using naive approach

This method checks for primality by dividing each number from 2 to n//2. If any divisor is found, number is not prime. This method is the least efficient and suitable only for small intervals.

Python
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: The code checks each number from x to y. Numbers ≤ 1 are skipped. For each number, it tests divisibility from 2 to i//2. If divisible, it’s not prime; otherwise, it’s added to the result list.


Python program to print all Prime numbers in an Interval
Article Tags :

Explore