Python| sympy.prime() 方法
在sympy.prime()方法的帮助下,我们可以找到第n 个素数,素数索引为prime(1) = 2, prime(2) = 3等。
Syntax: prime(n)
Parameter:
n – It denotes the nth prime number.
Returns: Returns the nth prime number.
示例 #1:
# import sympy
from sympy import prime
n = 5
# Use prime() method
nth_prime = prime(n)
print("The {}th prime is {}".format(n, nth_prime))
输出:
The 5th prime is 11
示例 #2:
# import sympy
from sympy import prime
n = 23
# Use prime() method
nth_prime = prime(n)
print("The {}rd prime is {}".format(n, nth_prime))
输出:
The 23rd prime is 83