A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number.
2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime [it is composite] since, 2 x 3 = 6
.
Source Code
# Python program to display all the prime numbers within an interval
lower = 900
upper = 1000
print["Prime numbers between", lower, "and", upper, "are:"]
for num in range[lower, upper + 1]:
# all prime numbers are greater than 1
if num > 1:
for i in range[2, num]:
if [num % i] == 0:
break
else:
print[num]
Output
Prime numbers between 900 and 1000 are: 907 911 919 929 937 941 947 953 967 971 977 983 991 997
Here, we store the interval as lower for lower interval and upper for upper interval, and find prime numbers in that range. Visit this page to learn how to check whether a number is prime or not.
First, identify your desired output.
Problem
Write a Python program that prints out all prime numbers up to 1000.
Understanding the math
The first step is to understand the definition of a prime. A prime number is a positive integer that is only divisible by one and…
A prime number is a natural number which is greater than 1 and has no positive divisor other than 1 and itself, such as 2, 3, 5, 7, 11, 13, and so on.
The user is given two integer numbers, lower value, and upper value. The task is to write the Python program for printing all the prime numbers between the given interval [or range].
To print all the prime numbers between the given interval, the user has to follow the following steps:
- Step 1: Loop through all the elements in the given range.
- Step 2: Check for each number if it has any factor between 1 and itself.
- Step 3: If yes, then the number is not prime, and it will move to the next number.
- Step 4: If no, it is the prime number, and the program will print it and check for the next number.
- Step 5: The loop will break when it is reached to the upper value.
Example: The Python Code to Print the Prime Number between the given Interval.
Output:
Please, Enter the Lowest Range Value: 14 Please, Enter the Upper Range Value: 97 The Prime Numbers in the range are: 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Conclusion
In this tutorial, we have shown how to write the code to print the prime numbers between the given interval of numbers.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given two positive integers start and end. The task is to write a Python program to print all Prime numbers in an Interval.
Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.
The idea to solve this problem is to iterate the val from start to end using a for loop and for every number, if it is greater than 1, check if it divides n. If we find any other number which divides, print that value.
Below is the Python implementation:
Python3
def
prime[x, y]:
prime_list
=
[]
for
i
in
range
[x, y]:
if
i
=
=
0
or
i
=
=
1
:
continue
else
:
for
j
in
range
[
2
,
int
[i
/
2
]
+
1
]:
if
i
%
j
=
=
0
:
break
else
:
prime_list.append[i]
return
prime_list
starting_range
=
2
ending_range
=
7
lst
=
prime[starting_range, ending_range]
if
len
[lst]
=
=
0
:
print
[
"There are no prime numbers in this range"
]
else
:
print
[
"The prime numbers in this range are: "
, lst]
Output:
The prime numbers in this range are: [2,3,5]
Time Complexity: O[N2], where N is the size of the range.
Auxiliary Space: O[N], since N extra space has been taken.
The above solution can be optimized using the Sieve of Eratosthenes. Please see print prime numbers in a range for details.