Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Big o 这段代码的时间复杂度很大_Big O_Time Complexity_Pseudocode - Fatal编程技术网

Big o 这段代码的时间复杂度很大

Big o 这段代码的时间复杂度很大,big-o,time-complexity,pseudocode,Big O,Time Complexity,Pseudocode,给定以下代码-: for(int i=1;ii的每个值的内部循环的总迭代次数将为 i = 1: j = 1, 2, 3 ..., n ---> total iterations = n i = 2: j = 1, 3, 5 ..., n ---> total iterations = n/2 if 2 divides n or one less otherwise i = 3: j = 1, 4, 7 ..., n ---> total iterations = n/3 if

给定以下代码-:


for(int i=1;ii的每个值的内部循环的总迭代次数将为

i = 1: j = 1, 2, 3 ..., n ---> total iterations = n
i = 2: j = 1, 3, 5 ..., n ---> total iterations = n/2 if 2 divides n or one less otherwise
i = 3: j = 1, 4, 7 ..., n ---> total iterations = n/3 if 3 divides n or one less otherwise
...
i = m: j = 1, 1 + m, ... , n ---> total iterations ~ n/m
...
1
因此,大约总迭代次数为
(n+n/2+n/3…+1)


该和是数值约为
ln(n)+C的值,因此总迭代次数约为
nln(n)
,并且由于所有对数都由一个常数关联,因此迭代次数将为
O(nlogn)

您可能会喜欢使用我在我的答案中使用的方法:但这需要时间。@Alp j随着每次迭代的增加而增加,而不是1。不确定,但它可能会有帮助:下面是@GrijeshChauhan:这个例子很好,但我正在寻找一个合适的数学证明,在那里我会将
日志
应用于基数2。也许我正在考虑迭代重现rrence解算,显然不能在这里应用。这正是我要找的。谢谢!@TheRedBlackTree你是对的,n(n+…+1)太多了。谢谢你抓住了!