Sunday, May 10, 2026
HomeLanguagesPython | sympy.prevprime() method

Python | sympy.prevprime() method

In sympy module, we can get the previous prime number for a given number n using sympy.prevprime() function. For n < 2^64 the answer is definitive; larger n values have a small probability of actually being pseudoprimes. 
 

Syntax:  sympy.prevprime()
Parameter:  n; number to be tested
Return:  previous prime value

Code #1: 

Python3




# Python program to get previous prime number
# using sympy.prevprime() method
  
# importing sympy module
from sympy import *
  
# calling prevprime function on different numbers
geek1 = prevprime(7)
geek2 = prevprime(13)
  
print(geek1)
print(geek2)


Output: 
 

5
11

Code #2: 

Python3




# Python program to check prime number
# using sympy.prevprime() method
  
# importing sympy module
import sympy.ntheory as nt
  
# calling prevprime function on different numbers
nt.prevprime(2)


Output: 
 

ValueError: no preceding primes

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS