Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Program to Check Prime Number

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Mar, 2023
Improve Article
Save Article

Given a positive integer N, The task is to write a Python program to check if the number is Prime or not in Python.

Examples: 

Input:  n = 11
Output: True


Input:  n = 1
Output: False

Explanation: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}. 

Prime Number Program in Python 

The idea to solve this problem is to iterate through all the numbers starting from 2 to (N/2) using a for loop and for every number check if it divides N. If we find any number that divides, we return false. If we did not find any number between 2 and N/2 which divides N then it means that N is prime and we will return True.

Python3




num = 11
# If given number is greater than 1
if num > 1:
    # Iterate from 2 to n / 2
    for i in range(2, int(num/2)+1):
        # If num is divisible by any number between
        # 2 and n / 2, it is not prime
        if (num % i) == 0:
            print(num, "is not a prime number")
            break
    else:
        print(num, "is a prime number")
else:
    print(num, "is not a prime number")

Output

11 is a prime number

Time complexity: O(n)
Auxiliary space: O(1)

Fastest Algorithm to Find Prime Numbers

Instead of checking till n, we can check till √n because a larger factor of n must be a multiple of a smaller factor that has been already checked. Now let’s see the code for the first optimization method ( i.e. checking till √n )

Python3




from math import sqrt
# n is the number to be check whether it is prime or not
n = 1
  
# this flag maintains status whether the n is prime or not
prime_flag = 0
  
if(n > 1):
    for i in range(2, int(sqrt(n)) + 1):
        if (n % i == 0):
            prime_flag = 1
            break
    if (prime_flag == 0):
        print("True")
    else:
        print("False")
else:
    print("False")

Output

False

Time complexity: O(sqrt(n))
Auxiliary space: O(1)

Check Prime Numbers Using recursion

We can also find the number prime or not using recursion. We can use the exact logic shown in method 2 but in a recursive way.

Python3




from math import sqrt
  
def Prime(number,itr):  #prime function to check given number prime or not
  if itr == 1:   #base condition
    return True
  if number % itr == 0#if given number divided by itr or not
    return False
  if Prime(number,itr-1) == False:   #Recursive function Call
    return False 
      
  return True
   
num = 13
  
itr = int(sqrt(num)+1)
  
print(Prime(num,itr))

Output

True

Time complexity: O(sqrt(n))
Auxiliary space: O(sqrt(n))

Check the Prime Trial Division Method

Python3




def is_prime_trial_division(n):
    # Check if the number is less than or equal to 1, return False if it is
    if n <= 1:
        return False
    # Loop through all numbers from 2 to
    # the square root of n (rounded down to the nearest integer)
    for i in range(2, int(n**0.5)+1):
        # If n is divisible by any of these numbers, return False
        if n % i == 0:
            return False
    # If n is not divisible by any of these numbers, return True
    return True
  
# Test the function with n = 11
print(is_prime_trial_division(11))

Output

True

Time complexity: O(sqrt(n))
Auxiliary space: O(sqrt(n))

RECOMMENDED ARTICLE – Analysis of Different Methods to find Prime Number in Python


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!