In this post, We will be going to see different ways to write a code in Python for checking whether a number is Prime or not, provided number > 1.
Prime Number is a natural number which is greater than 1 and that has no positive divisors other than 1 and number itself.
Examples :
Input: n = 17 Output: 17 is a Prime Number Input: n = 21 Output: 17 is not a Prime Number
Code 1: Run the loop from 1 to the given number and count the factors if factors are greater than 2 then it is not a prime otherwise prime.
# input number
number = 17
# maxCount variable set to 2
# because prime numbers
# have only 2 divisors
# i.e. 1 and itself
maxCount = 2
# initialise a variable
count = 0
# loop from 1 to number
for i in range(1, number + 1) :
# if number is divisible by any
# number (i) then increment
# count variable by 1
if number % i == 0 :
count += 1
# if value of count is greater than
# maxCount value then number is not
# prime otherwise prime
if count > maxCount :
print(number,"is not a Prime Number")
else:
print(number,"is a Prime Number")
Output :
17 is a Prime Number
Code 2: Run the loop from 2 to half of its number if any number in between divides the given number then the given number is not a prime otherwise prime.
# input number and flag assignment
# in one line
number, flag = 17, 0
# loop from 2 to half of the
# given number
for i in range(2, number // 2 + 1) :
# if number is divisible by any
# number (i) in b/w 2 to half of
# its number then set falg to 1
# and break out of the loop
if number % i == 0 :
flag = 1
break
# if flag is 1 then
# number is not prime
# otherwise prime
if flag :
print(number,"is not a Prime Number")
else:
print(number,"is a Prime Number")
Output :
17 is a Prime Number
Code 3: Use the concept of for-else loop.
# input number
number = 17
# loop from 2 to half of the
# given number
for i in range(2, number // 2 + 1) :
# if number is divisible by any
# number (i) in b/w 2 to half of
# its number then it is not
# a prime number
if number % i == 0 :
print(number,"is not a Prime Number")
break
# if not divisible then it is
# a prime number
else :
print(number,"is a Prime Number")
Output :
17 is a Prime Number
Code 4:
# main code
if __name__ == "__main__" :
# input number
number = 17
# loop from 2 to half of the
# given number
for i in range(2, number // 2 + 1) :
# if number is divisible by any
# number (i) in b/w 2 to half of
# its number then it is not
# a prime number
if number % i == 0 :
print(number,"is not a Prime Number")
break
# if not divisible then it is
# a prime number
else :
print(number,"is a Prime Number")
Output :
17 is a Prime Number
Code 5: Create a user defined function for checking number is prime or not.
# define a user defined
# function for checking
# a number is prime or
# not
def checkPrime(number) :
# loop from 2 to half of the
# given number
for i in range(2, number // 2 + 1) :
# if number is divisible by any
# number (i) in b/w 2 to half of
# its number then it is not
# a prime number
if number % i == 0 :
print(number,"is not a Prime Number")
break
# if not divisible then it is
# a prime number
else :
print(number,"is a Prime Number")
# main code
if __name__ == "__main__" :
# input number
number = 17
# function calling
checkPrime(number)
Output :
17 is a Prime Number