In this Tutorial -:

Understanding the Problem
Before diving into the code, it’s essential to have a clear understanding of what prime numbers are and the problem at hand. Prime numbers are integers greater than 1 that have no divisors other than 1 and themselves. Our goal is to write a program that can identify all the prime numbers within a given range specified by the user.
Developing the Algorithm
To find the prime numbers lying within a given range, we can simply perform following steps:
1. Loop over the given range.
2. Within loop, we pick each element and check whether its prime or not.
Writing the Python Code
Now, let’s implement the algorithm in Python. Below is the code snippet that accomplishes this task:
def find_primes(start, end):
primes = []
for num in range(start, end + 1):
if is_prime(num):
primes.append(num)
return primes
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
# Testing the function
start_range = int(input("Enter the starting range: "))
end_range = int(input("Enter the ending range: "))
prime_numbers = find_primes(start_range, end_range)
print("Prime numbers within the given range are:")
for prime in prime_numbers:
print(prime, end=" ")
O/P
Enter the starting range: 1
Enter the ending range: 100
Prime numbers within the given range are:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Code Explanation
In this implementation, we define a function is_prime that takes a number as input and checks whether it is prime or not. Here’s how it works:
1. If the number is less than 2, it is not prime (since prime numbers are greater than or equal to 2), so we return False.
2. We iterate from 2 up to the square root of the given number (int(number**0.5) + 1), checking if the number is divisible by any of the values in that range.
3. If the number is divisible by any of the values in the range, we return False.
4. If none of the values in the range divide the number, we return True, indicating that the number is prime.
5. Finally, we test the function by taking user input for a number and checking whether it is prime or not. The program then prints the corresponding message based on the result.
This implementation is efficient for checking individual numbers, but if you need to find prime numbers within a range, using the Sieve of Eratosthenes algorithm (as explained here) would be more efficient.
Dry Run
Let’s perform a dry run of the code with an example:
1. Suppose the user enters a starting range of 10 and an ending range of 30.
2. The find_primes function is called with start_range = 10 and end_range = 30.
3. The function initializes an empty list called primes.
4. The for loop iterates over each number in the range from 10 to 30 (inclusive).
- For the first iteration, num = 10.
- The is_prime function is called with number = 10.
- Since 10 is not less than 2, we proceed to the next step.
- The for loop in the is_prime function starts from 2 and iterates up to the square root of 10 (4 in this case).
- In the first iteration, i = 2. Since 10 % 2 is 0, we return False from the is_prime function.
- The is_prime function call evaluates to False, so 10 is not considered prime and is not appended to the primes list.
- The loop continues in a similar manner for the remaining numbers in the range.
- For the number 11, the is_prime function returns True, so it is appended to the primes list.
- The same happens for the numbers 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, and 30.
5. After checking all numbers in the range, the function returns the primes list, which contains [11, 13, 17, 19, 23, 29].
6. The prime numbers within the given range are printed to the console: 11, 13, 17, 19, 23, 29.
O/P:
Enter the starting range: 10
Enter the ending range: 30
Prime numbers within the given range are:
11 13 17 19 23 29
