Page Contents
In this tutorial we will write a Python program to check Prime number . Before reading further we recommend to read about below tutorial-
What is Prime number
A positive integer number which is greater than 1 and only divisible by 1 and that number is called Prime number .
Example :
- 2 is a prime number as 2 is only divisible by 1 and 3 .
- 3 is a prime number as 3 is only divisible by 1 and 3.
- 4 is not a prime number because it is not only divisible by 1 and 4 , it is also divisible by 2 .
Python program to check Prime number
Steps :
- First we will define a function named
is_prime()
to check if a number is prime number or not . This function will take a number as function argument . - Then we will define a flag variable as
True
. It means if the number is not divisible by any other number except 1 and that number then it is a prime number . - After that we will check if the given number is less than 2 or not . If the given number is less than 2 then we simply return
None
and exit from the function . - Then we will check if the given number is divisible by any other number except 1 and that number starting from 2 and ending at square root of the given number using for loop . To determine square root of the given number we need to import
math
module and usemath.sqrt()
function . - If the given number is divisible by any other number except 1 and that number then we simply change the value of the flag variable as
False
and exit from the loop usingbreak
statement . - After that we will return the flag variable .
- If the function return
None
then the given number is less than 2 , if the function returnTrue
then the given number is a prime number and if the function returnFalse
then the given number is not a prime number
import math
# defining a function to check
# if a given number is prime or not
def is_prime(number):
# declaring a flag variable
prime=True
# if the given number is less than 2
# then the function will return None
if number<2:
return None
else:
# trying to divide the number by the numbers
# starting from 2
for i in range(2,int(math.sqrt(number)+1)):
if number%i==0:
# if the number is divisable by any number
# staring from 2 then change the flag variable
# and exit immediately from the loop
prime=False
break
return prime
# calling the function
print(is_prime(1))
print(is_prime(7))
print(is_prime(9))
print(is_prime(34))
print(is_prime(113))
None True False False True
- In the above code first we have defined the
is_prime()
function which will check if the given number is prime or not . - As 1 is less than 2 so
is_prime(1)
has returnedNone
. - As 7 is only divisible by 1 and 7 so it is a prime number . For this reason
is_prime(7)
has returnedTrue
. - As 9 is not only divisible by 1 and 9 but also divisible by 3 so it is not a prime number . For this reason
is_prime(9)
has returnedFalse
. - As 34 is not only divisible by 1 and 34 but also divisible by 2 and 17 so it is not a prime number . For this reason
is_prime(34)
has returnedFalse
. - As 113 is only divisible by 1 and 113 so it is a prime number . For this reason
is_prime(113)
has returnedTrue
.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .