Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | simpy.prevprime() method

  • Last Updated : 03 Aug, 2021

In simpy 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
prevprime(7)
prevprime(13)

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

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!