What is sympy.nextprime() in Python?

What is the sympy library?

sympy is a library we use for symbolic mathematics in Python.

We use pip to install sympy. Let’s refer to the following command:

pip install sympy

The nextprime method

The nextprime method returns the ith prime number greater than the given number, where i is an integer.

The default value for i is 1. So, the nextprime method returns the first prime number greater than the given number.

Syntax

Let’s view the syntax of nextprime method.

sympy.nextprime(n, ith=1)

Parameters

  • n: This represents any number.
  • ith: This indicates the ordinal prime number greater than n to be retrieved.

Return value

The method returns the ith prime number greater than n.

Code

Let’s view the code below:

import sympy
n = 100
ith = 5
print("sympy.nextprime(%s, %s) = %s" % (n, ith, sympy.nextprime(n, ith)))

Code explanation

  • Line 1: We import the sympy package.
  • Line 3: We define n.
  • Line 5: We define i.
  • Line 6: We obtain the ith prime number larger than n using the sympy.nextprime() method.

Free Resources