Contents
Print Prime Numbers between Given Two Numbers
In this program, we read two numbers from user, and find all the prime numbers between these two numbers.
Input
m
, n
where m>0
, n>0
and m < n
Program
We write findPrimes()
function that takes m
and n
as parameters, and returns a list of prime numbers in the range [m, n)
.
Python Program
def findPrimes(m, n):
primes = []
for k in range(m,n):
#check if k is a prime number
i = 2
isPrime = True
while i < (k / i):
if k % i == 0:
isPrime = False
break
i += 1
#if k is a prime number, append it to result list
if isPrime:
primes.append(k)
return primes
m = int(input('Enter m : '))
n = int(input('Enter n : '))
result = findPrimes(m, n)
print(result)
Output #1
Enter m : 5
Enter n : 25
[5, 7, 9, 11, 13, 17, 19, 23]
Output #2
Enter m : 1000
Enter n : 1200
[1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193]
Related Tutorials
- Python – Check if Number is Armstrong
- Python – Largest of Three Numbers
- How to Get Number of Axes in Pandas DataFrame?
- Python – Factorial of a Number
- Python Program to Add Two Numbers
- Numpy sqrt() – Find Square Root of Numbers
- Python Complex Number – Initialize, Access
- Python – Sum of Two Numbers
- Python String – Find the number of overlapping occurrences of a substring
- How to Get Number of Elements in Pandas DataFrame?