0
0
PythonProgramBeginner · 2 min read

Python Program to Print All Prime Numbers in Range

You can print all prime numbers in a range using a loop and a check for divisibility with for num in range(start, end+1): and inside it checking if num is prime by testing divisors from 2 to int(num**0.5)+1.
📋

Examples

Inputstart=1, end=10
Output2 3 5 7
Inputstart=10, end=20
Output11 13 17 19
Inputstart=22, end=22
Output
🧠

How to Think About It

To find prime numbers in a range, start from the first number and check each number if it is prime. A number is prime if it is greater than 1 and has no divisors other than 1 and itself. To check this, try dividing the number by all integers from 2 up to its square root. If none divide evenly, the number is prime.
📐

Algorithm

1
Get the start and end values of the range
2
For each number from start to end inclusive, do:
3
If the number is less than 2, skip it
4
Check divisibility from 2 to the square root of the number
5
If no divisor found, print the number as prime
💻

Code

python
start = 1
end = 20
for num in range(start, end + 1):
    if num > 1:
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                break
        else:
            print(num, end=' ')
Output
2 3 5 7 11 13 17 19
🔍

Dry Run

Let's trace the program for the range 1 to 10.

1

Start loop with num=1

num=1 is not greater than 1, so skip.

2

num=2 check

Check divisors from 2 to sqrt(2)=1.4 (no divisors to check). No divisor found, print 2.

3

num=3 check

Check divisors 2 to sqrt(3)=1.7 (no divisors to check). No divisor found, print 3.

4

num=4 check

Check divisors 2 to 2. 4 % 2 == 0, break, do not print.

5

num=5 check

Check divisors 2 to 3. 5 % 2 != 0, no divisor found, print 5.

numdivisor checkeddivisible?print?
1N/AN/ANo
2N/ANoYes
3N/ANoYes
42YesNo
52,3NoYes
62YesNo
72,3NoYes
82YesNo
92,3Yes (3)No
102YesNo
💡

Why This Works

Step 1: Check numbers greater than 1

Prime numbers are greater than 1, so numbers less than 2 are skipped.

Step 2: Check divisibility up to square root

If a number has a divisor larger than its square root, it must also have a smaller one, so checking up to the square root is enough.

Step 3: Use else with for loop

The else block after the for loop runs only if no break occurs, meaning no divisor was found and the number is prime.

🔄

Alternative Approaches

Using a function to check primality
python
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

start, end = 1, 20
for num in range(start, end+1):
    if is_prime(num):
        print(num, end=' ')
This separates logic and improves readability but adds function call overhead.
Using Sieve of Eratosthenes
python
start, end = 1, 20
sieve = [True] * (end + 1)
sieve[0], sieve[1] = False, False
for i in range(2, int(end**0.5) + 1):
    if sieve[i]:
        for j in range(i*i, end+1, i):
            sieve[j] = False
for num in range(start, end+1):
    if sieve[num]:
        print(num, end=' ')
This is faster for large ranges but uses more memory and is more complex.

Complexity: O(n * sqrt(m)) time, O(1) space

Time Complexity

For each number n in the range, we check divisibility up to sqrt(n), so total time is roughly n times sqrt of the max number.

Space Complexity

The program uses constant extra space, only a few variables for loops and checks.

Which Approach is Fastest?

The Sieve of Eratosthenes is faster for large ranges but uses more memory, while the simple check is easier and uses less space.

ApproachTimeSpaceBest For
Simple divisibility checkO(n * sqrt(m))O(1)Small to medium ranges
Function-based checkO(n * sqrt(m))O(1)Improved readability, small ranges
Sieve of EratosthenesO(n log log n)O(n)Large ranges, performance critical
💡
Check divisibility only up to the square root of the number to save time.
⚠️
Checking divisibility up to the number itself instead of its square root, causing slow performance.