Last Updated : 11 Jul, 2025
Given a positive integer N, the task is to write a Python program to check if the number is Prime or not in Python. For example, given a number 29, it has no divisors other than 1 and 29 itself. Hence, it is a prime number.
Using sympy.isprime() methodNote: Negative numbers (e.g. -13) are not considered prime number.
In the sympy module, we can test whether a given number n is prime or not using sympy.isprime() function. For n < 264 the answer is definitive; larger n values have a small probability of actually being pseudoprimes. Before using sympy module, we need to install it using this command:
pip install sympyPython
from sympy import *
g1 = isprime(30)
g2 = isprime(13)
g3 = isprime(2)
print(g1)
print(g2)
print(g3)
Output
False True True
Explanation:
The code implements a basic approach to check if a number is prime or not, by traversing all the numbers from 2 to sqrt(n)+1 and checking if n is divisible by any of those numbers.
Python
import math
n = 11
if n <= 1:
print(False)
else:
is_prime = True
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
is_prime = False
break
print(is_prime)
Explanation:
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
Python
from math import sqrt
n = 17
p_fl = 0
if(n > 1):
for i in range(2, int(sqrt(n)) + 1):
if (n % i == 0):
p_fl = 1
break
if (p_fl == 0):
print("True")
else:
print("False")
else:
print("False")
Explanation:
We can use the Miller-Rabin Primality Test, a probabilistic method, to check if a number is prime by performing multiple rounds of testing, where each test verifies if a randomly chosen base witnesses the compositeness of the number.
Python
import random
n = 30
k = 5
if n <= 1:
print(False)
elif n <= 3:
print(True)
elif n % 2 == 0:
print(False)
else:
d = n - 1
while d % 2 == 0:
d //= 2
is_prime = True
for _ in range(k):
a = random.randint(2, n - 2)
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
while d != n - 1:
x = (x * x) % n
d *= 2
if x == 1:
is_prime = False
break
if x == n - 1:
break
if not is_prime:
break
print(is_prime)
n = 3
k = 5
if n <= 1:
print(False)
elif n <= 3:
print(True)
elif n % 2 == 0:
print(False)
else:
d = n - 1
while d % 2 == 0:
d //= 2
is_prime = True
for _ in range(k):
a = random.randint(2, n - 2)
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
while d != n - 1:
x = (x * x) % n
d *= 2
if x == 1:
is_prime = False
break
if x == n - 1:
break
if not is_prime:
break
print(is_prime)
Explanation:
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.
Python
from math import sqrt
def Prime(n, i):
if i == 1 or i == 2:
return True
if n % i == 0:
return False
if Prime(n, i - 1) == False:
return False
return True
n = 13
i = int(sqrt(n) + 1)
print(Prime(n, i))
Explanation:
Recommended Artilce – Analysis of Different Methods to find Prime Number in Python
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4