0
0
PythonProgramBeginner · 2 min read

Python Program to Print Prime Numbers in Range

You can print prime numbers in a range using a loop and checking divisibility with for num in range(start, end+1): and if all(num % i != 0 for i in range(2, int(num**0.5)+1)) to test primality.
📋

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 prime number is only divisible by 1 and itself. To check this, try dividing the number by all smaller numbers starting 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
Loop through each number from start to end.
3
For each number, check if it is less than 2; if yes, skip it.
4
Check divisibility of the number by all integers from 2 to its square root.
5
If no divisor is found, print the number as it is 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 example with start=10 and end=15 through the code

1

Start loop with num=10

Check if 10 > 1: yes Check divisors from 2 to 3 10 % 2 == 0 (divisible), so break, not prime

2

Next num=11

11 > 1: yes Check divisors 2 to 3 11 % 2 != 0, 11 % 3 != 0 No divisor found, print 11

3

Next num=12

12 > 1: yes 12 % 2 == 0, break, not prime

4

Next num=13

13 > 1: yes Check divisors 2 to 3 13 % 2 != 0, 13 % 3 != 0 No divisor found, print 13

5

Next num=14

14 > 1: yes 14 % 2 == 0, break, not prime

6

Next num=15

15 > 1: yes Check divisors 2 to 3 15 % 2 != 0, 15 % 3 == 0, break, not prime

NumberDivisible by any from 2 to sqrt(num)?Prime?
10Yes (2)No
11NoYes
12Yes (2)No
13NoYes
14Yes (2)No
15Yes (3)No
💡

Why This Works

Step 1: Check numbers greater than 1

Prime numbers are greater than 1, so numbers less than or equal to 1 are skipped.

Step 2: Test divisibility up to square root

Checking divisibility up to the square root is enough because if a number has a factor larger than its square root, it must have a smaller corresponding factor.

Step 3: Use loop with else for prime detection

If the loop completes without finding a divisor, the else block runs, confirming 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=' ')
Separates logic for clarity and reuse, easier to maintain.
Using list comprehension
python
start, end = 1, 20
primes = [num for num in range(start, end + 1) if num > 1 and all(num % i != 0 for i in range(2, int(num ** 0.5) + 1))]
print(*primes)
More concise and Pythonic, but less readable for beginners.

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

Time Complexity

For each number in the range (n), we check divisibility up to its square root (sqrt(n)), resulting in O(n * sqrt(n)) time.

Space Complexity

The program uses constant extra space O(1) as it only stores loop variables and prints results directly.

Which Approach is Fastest?

Using a function or list comprehension does not change time complexity but can improve readability or conciseness. For very large ranges, advanced methods like the Sieve of Eratosthenes are faster.

ApproachTimeSpaceBest For
Loop with divisibility checkO(n * sqrt(n))O(1)Simple and clear for small ranges
Function-based checkO(n * sqrt(n))O(1)Reusable and clearer code
List comprehensionO(n * sqrt(n))O(n)Concise code, less beginner-friendly
💡
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, which slows down the program.