Python Program to Print Prime Numbers In a Given Range
Find the Prime Numbers in a Given Range in Python
Given two integer as Limits, low and high, the objective is to write a code to in Python Find Prime Numbers in a Given Range in Python Language. To do so we’ll use nested loops to check for the Prime while Iterating through the range.Example
Input : low = 2 , high = 10
Output : 2 3 5 7
Find the Prime Numbers in a Given Interval in Python
Given two integer variables for range, the objective is to check for all the prime number that lay in the given interval. The two input integers will act as the interval limits low and high. In order to check which iterating, we’ll use nested loops. The outer loop will iterate through the numbers while the inner loop will check for Prime. Here are some of the methods used to solve the above mentioned problem in python language
- Method 1: Using inner loop Range as [2, number-1].
- Method 2: Using inner loop Range as [2, number/2].
- Method 3: Using inner loop Range as [2, sqrt(number)].
- Method 4: Using inner loop Range as [3, sqrt(number), 2].
We’ll discuss the above mentioned methods in the upcoming sections below. Check out Python Program to check for Prime. Check out the definition in the blue box below.
Method 1: Using inner loop Range as [2, number-1]
Working
For two integer inputs as low and high limits of the interval,
- Run a for loop to iterate through all the numbers.
- Run a Nested for loop to check for prime or not.
Let’s implement the above logic in Python Language. Lets see the implementation now for python program to find prime numbers in range
Python Code
# python find prime numbers in range low, high = 2, 10 primes = [] for i in range(low, high + 1): flag = 0 if i < 2: continue if i == 2: primes.append(2) continue for x in range(2, i): if i % x == 0: flag = 1 break if flag == 0: primes.append(i) print(primes)
Output
[2, 3, 5, 7]
Method 2: Using inner loop Range as [2, number/2]
Working
For two integer inputs as limits, we perform the following main operations,
- Run a for loop to iterate through the numbers in a given interval.
- Run a nested while to check for prime by checking if the number has any other factors in the range [2, number/2].
Let’s implement the above logic in Python Language.
Python Code
low, high = 2, 10 primes = [2] for num in range(low, high + 1): flag = 0 if num < 2: flag = 1 if num % 2 == 0: continue iter = 2 while iter < int(num / 2): if num % iter == 0: flag = 1 break iter += 1 if flag == 0: primes.append(num) print(primes)
Output
[2, 3, 5, 7]
Method 3: Using inner loop Range as [2, sqrt(number)]
Working
For two integer inputs as low and high limits of the interval, we perform the following
- Run a for loop to iterate through the number in the given interval.
- Run a nested while loop to check for prime or not.
- We do so by checking if the number has any factors in the range [2, sqrt(number)].
Let’s implement the above logic in Python Language.
Python Code
low, high = 2, 10 primes = [2, 3] for num in range(low, high + 1): flag = 0 if num < 2: flag = 1 if num % 2 == 0: continue if num % 3 == 0: continue iter = 2 while iter < int(pow(num, 0.5)): if num % iter == 0: flag = 1 break iter += 1 if flag == 0: primes.append(num) print(primes)
Output
[2, 3, 5, 7]
Method 4: Using inner loop Range as [3, sqrt(number), 2]
Working
This method is similar to the one above but here we’ll use 2 as a step to skip the even numbers.
For a given interval as [low, high], we do the following
- Run a for loop to iterate through the numbers that lay in the input interval.
- Run a nest while eith step size as 2 from 3 to the square root of number and check for factors of the number in that interval.
Let’s implement the above logic in Python Language.
Python Code
low, high = 2, 10 primes = [2, 3] for num in range(low, high + 1): flag = 0 if num < 2: flag = 1 if num % 2 == 0: continue if num % 3 == 0: continue iter = 3 while iter < int(pow(num, 0.5)): if num % iter == 0: flag = 1 break iter += 2 if flag == 0: primes.append(num) print(primes)
Output
[2, 3, 5, 7]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment
n1 = int(input())
n2 = int(input())
for num in range(n1,n2+1):
if num>1:
for i in range(2,num):
if num%i==0:
break
else:
print(num)
#code by abhishek patil
n=int(input())
if(n==1):
print(‘it is neither prime nor composite’)
for i in range(2,n):
if(n%i==0):
print(“it is a composite”)
break
if((i+1)==n and n%(i+1)==0):
print(‘it is a prime number’)
This is the correct code:
first = int(input(“Enter the first number:”))
second = int(input(“Enter the Second Number:”))
for i in range(first, second+1):
for j in range(2, (i//2)+1):
if i % j == 0:
break
else:
print(“Prime Number”, i)
a=[1]
b=[1]
m=int(input(“Enter the lower interval: “))
n=int(input(“Enter the higher interval: “))
x=m
while m<=x<=n:
a.append(x)
k=2
while k<x:
c=x%k
if c==0:
b.append(x)
k=k+1
x=x+1
a=set(a)
b=set(b)
prime=a^b
prime=list(prime)
print(prime)
This maynot be the correct way to find the prime numbers. However this code also gives you the list of prime numbers.
Best and simple way to solve this problem and printing the result in a single line within a list.
Program Code:
a = []
n1 = int(input(“enter 1st number: “))
n2 = int(input(“enter 2nd number: “))
for i in range(n1,n2):
for j in range(2,i-1):
if i%j==0:
break
else:
a.append(i)
print(“Prime numbers are: “,a)
Output:
enter 1st number: 21
enter 2nd number: 45
Prime numbers are: [23, 29, 31, 37, 41, 43]
n1 = int(input(“Enter the first number:”))
n2 = int(input(“Enter the Second Number:”))
for i in range(n1, n2):
for j in range(2, i):
if i % j == 0:
break
else:
print(“Prime Number”, i)
def prime(n):
i = 2
while i < n:
if (n%i) == 0:
return 0
i+= 1
return 1
a = int(input("a: "))
b = int(input("b: "))
c = min([a,b])
d = max([a,b])
e = []
for i in range(c,d+1):
if prime(i) == 1:
e.append(i)
print("prime numbers: " + str(e))
first = int(input(“Enter the first number:”))
second = int(input(“Enter the Second Number:”))
for i in range(first, second+1):
for j in range(2, i):
if i % j == 0:
break
else:
print(“Prime Number”, i)
Above code will give error since range() does not accept decimal numbers it is used only for integers:
So the code will be :
l=int(input(“Enter lower limit”))
u=int(input(“Enter upper limit”))
for i in range(l,u+1):
flag=0
mid=i//2
for j in range(2,mid+1):
if(i%j==0):
flag=1
break
if(flag==0):
print(i)
Above mentioned program gives wrong output
The Correct Code is:
f=int(input())
s=int(input())
for i in range(f,s):
for j in range(2,i):
if i%j == 0:
break
else:
print(“Prime Number”, i)
f=int(input(“Enter starting point: “))
l=int(input(“Enter ending point: “))
arr=[]
for i in range(f,l):
for j in range(2,i//2):
if i%j==0:
break
else:
arr.append(i)
print(“prime numbers are: “,arr)
lower = int(input(“Enter the lower interval:”))
upper = int(input(“Enter the upper interval:”))
for num in range(lower,upper):
num>1
for i in range(2, num):
if num % i == 0:
break
else:
print(“Prime Number”, num)
PrimeList=[ ]
lower = int(input(“Enter the lower interval:”))
upper = int(input(“Enter the upper interval:”))
for num in range(lower,upper):
for i in range(2, num):
if num % i == 0:
break
else:
PrimeList.append(num)
print(“Prime Number”, PrimeList)
#modified one, eliminates empty list and negative numbers
PrimeList=[ ]
lower = int(input(“Enter the lower interval:”))
upper = int(input(“Enter the upper interval:”))
if lower0:
print(“Prime Numbers:”, PrimeList)
else:
print(“No Prime numbers within the range”)
This program provides the wrong output, The number 4 is not a prime number because it can be divided evenly by 4, 2, and 1.
Enter the first number:1
Enter the Second Number:25
Prime Number 1
Prime Number 2
Prime Number 3
Prime Number 4
Prime Number 5
Prime Number 7
Prime Number 11
Prime Number 13
Prime Number 17
Prime Number 19
Prime Number 23
n=1
n2=25
for i in range(n,n2+1):
if i==1:
continue
f=1
for j in range(2,i//2+1):
if i%j==0:
f=0
break
if f==1:
print(i,end=” “)
Thanks shubham for contributing your code