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

6 COMMENTS

Most Popular

Dominic
32702 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7076 POSTS0 COMMENTS
Nicole Veronica
12211 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12303 POSTS0 COMMENTS
Shaida Kate Naidoo
7197 POSTS0 COMMENTS
Ted Musemwa
7456 POSTS0 COMMENTS
Thapelo Manthata
7174 POSTS0 COMMENTS
Umr Jansen
7161 POSTS0 COMMENTS