What is sympy.prevprime() in Python?

What is sympy library?

sympy is a library used for symbolic mathematics.

pip can be used to install sympy. You can refer to the following command.

pip install sympy

The prevprime method

The prevprime method returns the largest prime number that is less than the given number. The given number has to be a positive number.

Syntax

sympy.prevprime(n)

Parameters

  • n: This can be any positive number.

Return value

The method returns the largest prime number that is less than the given number.

Code

# imports sympy library
import sympy
# declares variable n with value = 5
n = 5
# prints the largest prime number
# less than the given number n
print("sympy.prevprime(%s) = %s" % (n, sympy.prevprime(n)))

Code explanation

  • Line 1: We import the sympy package.
  • Line 3: We define n.
  • Line 5: We obtain the largest prime number less than n using the sympy.prevprime() method.

Free Resources