算法的复杂度(威尔逊检验)

时间:2019-06-08 02:36:54

标签: python complexity-theory

请问用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

1 个答案:

答案 0 :(得分:0)

要简单地计算wilson函数的复杂度,其复杂度通常基于处理器中运行的“指令”,因此从根本上来说,只有循环会影响复杂度,wilson具有一个if和factorielle的复杂度为1,所以O(1 + O(factorielle))。

因此,为了计算factorielle的复杂度,我们对循环进行了分析,您可以看到该循环运行了n-1次,对吗?因此factorielle的复杂度为O(n-1),这使得factorielle的复杂度为O(n),因为,它仅取决于n到代码运行的时间