Thursday, October 2, 2025
HomeLanguagesPython | sympy.isprime() method

Python | sympy.isprime() method

In the sympy module, we can test whether a given number n is prime or not using sympy.isprime() function. For n < 2^64 the answer is definitive; larger n values have a small probability of actually being pseudoprimes. 

Note that Negative numbers (e.g. -13) are not considered prime number.

Syntax:  sympy.isprime()
Parameter:  n; number to be tested
Return:  bool value result 

Code #1:  

Python3




# Python program to check prime number
# using sympy.isprime() method
 
# importing sympy module
from sympy import *
 
# calling isprime function on different numbers
geek1 = isprime(30)
geek2 = isprime(13)
geek3 = isprime(2)
 
print(geek1) # check for 30 is prime or not
print(geek2) # check for 13 is prime or not
print(geek3) # check for 2 is prime or not


Output: 

False
True
True

Code #2:  

Python3




# Python program to check prime number
# using sympy.isprime() method
 
# importing sympy module
import sympy.ntheory as nt
 
# calling isprime function on different numbers
geek1 = nt.isprime(30)
geek2 = nt.isprime(13)
geek3 = nt.isprime(2)
 
print(geek1) # check for 30 is prime or not
print(geek2) # check for 13 is prime or not
print(geek3) # check for 2 is prime or not


Output: 

False
True
True

 

RELATED ARTICLES

Most Popular

Dominic
32330 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11926 POSTS0 COMMENTS
Shaida Kate Naidoo
6817 POSTS0 COMMENTS
Ted Musemwa
7078 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6774 POSTS0 COMMENTS