请问用O(..)表示Wilson函数的复杂度是什么?
#fonction calculant le factorielle de n (n!) :
def factorielle(n):
p=1
for i in range(2,n+1):
p=p*i
return p
#fonction testant la primalité de p en utilisant le théorème de wilson :
def wilson(p):
if factorielle(p-1)%p==p-1:
return True
return False
答案 0 :(得分:0)
要简单地计算wilson
函数的复杂度,其复杂度通常基于处理器中运行的“指令”,因此从根本上来说,只有循环会影响复杂度,wilson
具有一个if和factorielle
的复杂度为1,所以O(1 + O(factorielle
))。
因此,为了计算factorielle
的复杂度,我们对循环进行了分析,您可以看到该循环运行了n-1次,对吗?因此factorielle
的复杂度为O(n-1),这使得factorielle
的复杂度为O(n),因为,它仅取决于n到代码运行的时间