Python Lab · Experiment 9
Print all prime numbers in an interval
PythonWeek 2JNTUH R22 · CS206ES
Aim
To write a Python program that reads a lower and an upper limit and prints all the prime numbers between them, using break to stop checking a number as soon as a divisor is found.
Algorithm
- Read the lower limit and the upper limit.
- If the lower limit is greater than the upper limit, print a message and stop.
- For each number num from the lower limit to the upper limit:
- Skip num if it is less than 2, because 0 and 1 are not prime.
- Try each divisor i from 2 to num − 1. If num % i is 0, num is not prime, so
breakout of the inner loop. - If the inner loop finished without a
break, num is prime. Print it and add 1 to the count. - Print how many primes were found.
Program
lower = int(input("Enter the lower limit: "))
upper = int(input("Enter the upper limit: "))
if lower > upper:
print("Lower limit must not be greater than upper limit")
else:
print(f"Prime numbers between {lower} and {upper}:")
count = 0
for num in range(lower, upper + 1):
if num < 2:
continue # 0, 1 and negative numbers are not prime
for i in range(2, num):
if num % i == 0:
break # found a divisor, so num is not prime
else:
# this else runs only when the loop did not break
print(num, end=" ")
count += 1
print()
print("Count:", count)Output
Interval 10 to 50
Enter the lower limit: 10
Enter the upper limit: 50
Prime numbers between 10 and 50:
11 13 17 19 23 29 31 37 41 43 47
Count: 11Interval 1 to 20
Enter the lower limit: 1
Enter the upper limit: 20
Prime numbers between 1 and 20:
2 3 5 7 11 13 17 19
Count: 8Recorded by running this exact program. Typed input appears after each prompt.
How it works
The else here belongs to the inner for loop, not to the if. A loop's else block runs when the loop ends normally, and is skipped when the loop ends with break. That makes it a natural way to say "no divisor was found".
break saves time: once one divisor is found there is no need to test the rest.
Testing divisors only up to √num is enough, because if num = a × b, one of a and b is at most √num. Changing the inner loop to range(2, int(num ** 0.5) + 1) gives the same primes much faster for large intervals.
Common mistakes
Indenting the
elseto line up with theifinstead of thefor. Then the number is printed once for every divisor that does not divide it.Forgetting to skip 1. With no divisors to try, the inner loop ends without
break, so 1 would be printed as prime.Writing
range(lower, upper), which leaves out the upper limit itself.Using a flag variable but forgetting to reset it for each new number. Set it again at the start of every pass of the outer loop.
Viva questions
Try to answer each one aloud before you open it.
What is a prime number?
A whole number greater than 1 whose only divisors are 1 and itself. 2, 3, 5, 7 and 11 are prime; 1 is not prime.
What does break do?
It ends the nearest enclosing loop at once. Control goes to the first statement after that loop.
When does the else block of a for loop run?
When the loop finishes going through all its items without hitting a
break. Ifbreakruns, theelseis skipped.What is the difference between break and continue?
breakleaves the loop completely.continueskips the rest of the current pass and goes on with the next item.Why is it enough to check divisors up to the square root of the number?
If n = a × b and both a and b were bigger than √n, their product would be bigger than n. So every composite number has a divisor no bigger than √n.