Python Examples

Python Program to Check Prime Number

A prime number is a positive number that has only two factors, 1 and the number itself. Numbers such as 2, 3, 5, 7, 11, etc., are prime numbers as they do not have any other factors other than 1 and themselves. When creating the code in Python prime number that can check whether a number input is a prime number or not, the code will first check whether it is positive or not.

python prime numberOnce checked, the next thing to confirm is whether the number is equal to 1 or greater than it. Only numbers greater than 1 are checked by the main program to confirm whether they are prime or not.   

Prime Number Program in Python

Example 1: Using a flag variable.

                    

num = int (input (“Enter the desired number: “)

flag = False

if num > 1:

              for i in range (2, num):

                             if (num % i) == 0:

                                            flag = True

                                            break

if flag:

              print (“The number entered is not a prime number”)

else:

              print (“The number entered is a prime number”)

Here, the default value of the flag variable is set as False. When the value of the flag returns to be True, then the program breaks out of the loop. The value of the flag variable is true when the provided number gives the remainder zero with any number lesser than it and greater than zero.

Example 2: Using a for…else statement

                    

num = int (input (“Enter the desired number: “)

if num > 1:         

              for i in range (2, num):

                             if (num% i) == 0:

                                            print (“The provided number is not a prime number”)

                            else:

                                            print (“The number is a prime number”)

else:

              print (“The number is not a prime number”)

Here, instead of using the flag variable, we set conditions using the if…else statement to check whether a number is prime or not. When no factors of the given number are found, then the number is said to be prime.  

Source Code: 

Python Program to check Prime Number

                    

def is_prime (n):

              if n <= 1:

                             return false

              for i in range (2,n):

                             if n % i == 0:

                                            return false 

              return True

count = 0

for n in i range (1, 1000)

              x = is_prime(n)

              c += x

print (x)

print (“Total number of prime numbers present in the range: “, c)

In the code above, all the numbers that are present between 1 and 1000 are checked to verify which are prime numbers. Note that such a code may take longer to compute, depending upon the upper and lower limit of the range between which the prime numbers need to be calculated. 

To reduce the computation time of the Python prime number program to find the prime numbers that are present between a given range, the following method can be used. 

In this method, we can reduce the numbers with which the given number need to be divided. It can be done by finding the root of the given number. 

If the given number is divisible by any number present between 2 and its square root, then that means it is not a prime number. This happens because the square root of a number acts as a mirror between its factors. That is, the factors that lie on either side of the square root of the number are the same. Take the following example. 

64

1 x 64 = 64 

2 x 32 = 64 

4 x 16 = 64

8 x 8 = 64

16 x 4 = 64 

32 x 2 = 64

64 x 1 = 64 

Here, the factors before and after 8 x 8 reflect each other. 

                    

import math 

def is_prime (n):

              if n <= 1:

                             return False 

              max_div = math.floor (math.sqrt (n))

              for i in range (2, 1 + max_div):

                             if n % i == 0:

                                            return False 

              return True 

count = 0

for n in range (1, 1000)

              x = is_prime (n)

              c += x    

              print (x) 

print (“Total number of prime numbers in the given range: “, c)

In the code, note that we have imported the math library as we need to use the function to find the square root of the given number. The rest of the code works similarly to the ones given above. Such a code will take half the time as the original code as now the given number needs to be divided with half the numbers. 

Another method to check whether a number is prime or not is to first check whether it is an even number, that is if it is divisible by 2 or not. This way, the numbers whose square root needs to be calculated will be reduced in half, further reducing the compilation and execution time of the program. 

FAQs on Python Prime Number

How do you find prime numbers in Python?

To find the prime numbers in Python, the remainder of the number being divided by all numbers lying between 1 and the number itself is obtained. If any remainder is equal to zero, that means that the number is not a prime number.

Is the prime number function Python?

There is no specifically provided Python prime number provided in the Python library.  

How do you find the prime number from 1 to 100 in Python?

To find all the prime numbers that lie between an upper and lower limit, such as 1 and 100, these limits will need to be defined. A FOR loop will be used to calculate the Python prime numbers that make the code iterate till the upper limit is reached. A sample code is provided below.

                    

lower = 1

upper = 100

for num in range (lower, upper+1)

if num > 1:

              for i in range (2, num)

                             if (num % i) == 0

                                            break

                             else:

                                            print (num)

What is the logic of prime numbers in Python?

Using the modulus operator, it is checked if the number gives remainder zero when divided with numbers lesser than it and greater than zero.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.