Given the range of integers between M and N your task is to write a Python program that will print out all the prime numbers between M and N.
A prime number is a number that has only two factors 1 and the number itself and none of the other than these numbers can divide the given number, then it is referred to as the prime number.
Let's look into the Python program now.
Write A Python Program To List Out All Prime Numbers Between Two Integers M And N
Let's take two variables M and N, ask a user to enter the two integer values using the input function call and then convert them to the integers.
M = int(input("Enter Value of M : "))
N = int(input("Enter Value of N : "))
Once we get the integer inputs in the program use the if statement to check if M is less than 2 or also check if M is greater than N, Print invalid input and exit the program.
if M < 2 or M > N:
print("Invalid Input : Prime Numbers Start from 2 (or) M should be less than N")
exit(-1)
Take an empty list variable named as prime_numbers, and using the for loop iterate from M to N value ranges. Take the square root of the number by raising it to the power of 0.5 i.e, (1/2), convert it to an integer and add one to it.
Once we get the numerator range use another for loop to range from 2 to the square root of the numerator. Now using the if statement try modulating the numerator by the denominator and check if the remainder is 0, If the division is successful then for loop.
Using the for loop else part append the numerator to the list of prime numbers variable prime_numbers. The else block signifies that you have found the numerator as the prime number.
prime_numbers = []
for numerator in range(M, N):
sqrt = int(numerator ** 0.5) + 1
for denominator in range(2, sqrt):
if numerator % denominator == 0:
break
else:
prime_numbers.append(numerator)
Once the for loop completes prime_numbers a list variable will store the the all the prime numbers between M and N. now using the the print statement print the prime numbers between M and N, print the variable prime_numbers
print(f"Prime Numbers Between {M} and {N} are...")
print(prime_numbers)
Code
===================
M = int(input("Enter Value of M : "))
N = int(input("Enter Value of N : "))
if M < 2 or M > N:
print("Invalid Input : Prime Numbers Start from 2 (or) M should be less than N")
exit(-1)
prime_numbers = []
for numerator in range(M, N):
sqrt = int(numerator ** 0.5) + 1
for denominator in range(2, sqrt):
if numerator % denominator == 0:
break
else:
prime_numbers.append(numerator)
print(f"Prime Numbers Between {M} and {N} are...")
print(prime_numbers)
OUTPUT
==============
Enter Value of M : -10
Enter Value of N : 10
Invalid Input : Prime Numbers Start from 2 (or) M should be less than N
Output
==============
Enter Value of M : 500
Enter Value of N : 200
Invalid Input : Prime Numbers Start from 2 (or) M should be less than N
Output
============
Enter Value of M : 2
Enter Value of N : 100
Prime Numbers Between 2 and 100 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]
Output
=============
Enter Value of M : 50
Enter Value of N : 700
Prime Numbers Between 50 and 700 are...
[53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691]
Conclusion
===================
Provide the range values M and N as input to Python Program, execute the above program by yourself.
Comment down below if you have any suggestions / queries regarding the above program.
0 Comments