Python Program to check if a Number is Prime or not

In this Python example, we will discuss how to check if any given number is a prime number or not. Let’s begin.

1. What is a Prime Number?

A prime number is a number that can only be divided by itself and 1 without remainders. A prime number is a number greater than 1 with only two factors – themselves and 1.

According to Wikipedia
EXAMPLE:
Input :  13
Output : 13 is a Prime Number
Explanation : 13 has only two factors; 1 and itself

Input : 10
Output : 10 is not a Prime Number 
Explanation : 10 has more than two factors; 1, 2, 5 and itself

Some of the topics which will be helpful for understanding the program implementation better are:


2. Basic Program to Check Prime Number

  • In this method, we will check if the input value is 1 or not; if it is more than 1 then only we will proceed further.
  • Using a for loop, we will divide the input number from 2 to input-1. If any factor lies in that range, then the input number is not prime, and we will break the loop.
  • If any factor does not lie in that range, then we can say that the input number is a prime number.

Let’s implement the code and see how it works.

#Python Program to Check if a number is prime or not

def check_prime(n):    
  if n > 1:
    for i in range(2, n):
      if (n % i) == 0:           
          return True
          break
  else:
    return "Input number must be greater than 1"

n = int(input("Enter the number to check if it is Prime: "))

if check_prime(n):
  print(n, "is not a Prime Number.")
else:
  print(n, "is a Prime Number")
Output
Enter the number to check if it is Prime: 12
12 is not a Prime Number.

There is a possibility or the improvement in the above code

  • We can also find a number that the given number is prime or not using the loop with a range from 2 to n/2; divide the input number from 2 to n/2.
  • If any factor lies in that range, then the input number is not prime, and we will break the loop.
  • If any factor does not lie in that range, then we can say that the input number is a prime number.
#Python Program to Check if a number is prime or not using half range
n = int(input("Enter the number to check if it is Prime or not: "))
if n > 1:
  for i in range(2, int(n/2)+1):     
    if n % i == 0:
      print(n, "is not a prime number")
      break
  
  else:
    print(n, "is a prime number")
 
else:
  print(n, "is not a prime number")

3. Check if Number is Prime or not using sqrt

In this method, we take the help of the sqrt function inside the math module.
Rather than checking the loop condition till n, we can check it up to √n. The reason for doing this is because any input number n will have larger factors as multiple of smaller factors that are already checked.

Let’s implement and find out the working of the code.

#Python Program to Check if n umber is prime or not using sqrt
from math import sqrt

n = int(input("Enter the number to check for Prime: "))
 
if n > 1:
  for i in range(2, int(sqrt(n)) + 1):
    if (n % i == 0):
      print("This is Not a Prime Number")
      break
  else:
    print("This is a Prime Number")
else:
  print("Invalid Input")
Output
Enter the number to check for Prime: 31
This is a Prime Number

4. Conclusion

In this article, we have covered the Python program to check whether a given number is Prime or not using a basic for loop method and sqrt function.


Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x