Python| simpy.prevprime() 方法
在 simpy 模块中,我们可以使用 sympy.prevprime()函数获取给定数字 n 的前一个素数。对于 n < 2^64,答案是确定的;较大的 n 值实际上是伪素数的概率很小。
Syntax: sympy.prevprime()
Parameter: n; number to be tested
Return: previous prime value
代码#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)
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)
输出:
5
11
代码#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)
输出:
ValueError: no preceding primes