Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Find the number of prime numbers within a given range of numbers
When it is required to find the prime numbers within a given range of numbers, the range is entered and it is iterated over. The % modulus operator is used to check divisibility and identify prime numbers.
What is a Prime Number?
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11 are prime numbers.
Basic Approach
Below is a demonstration of finding prime numbers in a range ?
lower_range = 670
upper_range = 699
print("The lower and upper range are :")
print(lower_range, upper_range)
print("The prime numbers between", lower_range, "and", upper_range, "are:")
for num in range(lower_range, upper_range + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
The lower and upper range are : 670 699 The prime numbers between 670 and 699 are: 673 677 683 691
Optimized Method
We can optimize the algorithm by checking divisors only up to the square root of the number ?
import math
def is_prime(n):
if n < 2:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
lower_range = 10
upper_range = 30
print(f"Prime numbers between {lower_range} and {upper_range}:")
prime_count = 0
for num in range(lower_range, upper_range + 1):
if is_prime(num):
print(num, end=" ")
prime_count += 1
print(f"\nTotal prime numbers found: {prime_count}")
Prime numbers between 10 and 30: 11 13 17 19 23 29 Total prime numbers found: 6
How It Works
The algorithm follows these steps:
Numbers less than 2 are not prime by definition
For each number, check divisibility from 2 to its square root
If any divisor is found, the number is not prime
If no divisors are found, the number is prime
The
elseclause of theforloop executes only when the loop completes without breaking
Complete Example with User Input
Here's a complete program that accepts user input for the range ?
def find_primes_in_range(start, end):
primes = []
for num in range(start, end + 1):
if num > 1:
is_prime = True
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
primes.append(num)
return primes
# Example usage
start_range = 20
end_range = 50
prime_numbers = find_primes_in_range(start_range, end_range)
print(f"Prime numbers between {start_range} and {end_range}:")
print(prime_numbers)
print(f"Count: {len(prime_numbers)}")
Prime numbers between 20 and 50: [23, 29, 31, 37, 41, 43, 47] Count: 7
Conclusion
Finding prime numbers in a range involves checking each number for divisibility. The optimized approach checks only up to the square root, significantly improving performance for larger numbers. The for-else construct in Python provides an elegant way to handle the prime detection logic.
