因为变量i,我在这一点上遇到了一些麻烦。我只是不确定当它处于第二个while循环时如何对待它。对于我的外部循环,我理解它将运行log_4(n ^ 2)次迭代。对于内部while循环,我计算了迭代次数为(2n ^ 3 - 3)/ i。我正在努力研究如何将这两者放在一起以获得此功能的总体复杂性。非常感谢任何输入!
function p(n)
i = 1;
while i < n^2 do
j = 3;
while j < 2n^3 do
j = j + i;
end
i = 4i;
end
答案 0 :(得分:1)
我不擅长数学,但我想回答这个问题。
首先让我们从第一次迭代开始计算:
总共j增加: (2n ^ 3)+(2n ^ 3)/ 4 +(2n ^ 3)/ 16 +(2n ^ 3)/ 64 + ... +(2n ^ 3)/(n ^ 2)次。 那就是:
2n^3*(1+1/4+1/16+1/64+1/256+...+1/(n^2))
= 2n^3((1-(1/4)^(log_4(n^2)))/(1-(1/4))) // sum of geometric progression
= 2n^3 * (1-1/n^2) * 4/3
所以它是O(n ^ 3)。
答案 1 :(得分:1)
对于内循环,存在i = 1的情况,一次开始。 对于i = 1,内循环有大约2n ^ 3次迭代,我们可以说第一次运行外循环的复杂度为O(n ^ 3)。
请注意,由于我们正在尝试计算复杂度,我将摆脱常量和系数。
那么,对于i的其他值,内循环的迭代次数约为n ^ 3 / i。因此,当我增长时,迭代次数将大大减少。最终,对于i~ = n ^ 2的最后一个值,它将为n。
所以,现在,我们有一个像 n ^ 3 + ..... + n 这样的总和,它给了我们完全的复杂性。 此求和中的项数为log_4(n ^ 2)= 2log_4(n),比如log_4(n)。
通常,我们知道n ^ 3 + n ^ 2 + n与n ^ 3相同。但问题是我们在这种情况下能想到同样的情况吗?因为在这种情况下,有更多的术语和术语数量取决于n。我们来看看。
即使所有项都是n ^ 3类型,结果也是log_4(n)* n ^ 3。 但是,在这个系列中几何上下降的其他术语的价值,并不是n ^ 3。 log_4(n)对于人们通常使用的大量数字来说也是非常小的值。实际上人们不能简单地忽略它,但当我们一起考虑它是一个小数字和其他术语时。急剧减少;你可以忽略log_4(n)和我们可以说复杂度是O(n ^ 3)。
这不是一个确切的数学解决方案,但为了方便您,如果您确定我们正在做什么,我们可以使用这种估算方法。这就是我的观点以及为什么我会这样解释。
如果你正在寻找更具体的东西,你可以说它在O(n ^ 3)和O(log_4(n)* n ^ 3)之间。
另外,我已经为不同的n值计算了一些实验值。您可以看到数字在代码中的行为方式,以及迭代次数与n ^ 3之间的关系。结果如下:
Test #1:
n: 15
n^2: 225, n^3: 3375
...i=1, added 3375 iterations
...i=4, added 843 iterations
...i=16, added 210 iterations
...i=64, added 52 iterations
Total # of iterations for this test case: 4480
Test #2:
n: 56
n^2: 3136, n^3: 175616
...i=1, added 175616 iterations
...i=4, added 43904 iterations
...i=16, added 10976 iterations
...i=64, added 2744 iterations
...i=256, added 686 iterations
...i=1024, added 171 iterations
Total # of iterations for this test case: 234097
Test #3:
n: 136
n^2: 18496, n^3: 2515456
...i=1, added 2515456 iterations
...i=4, added 628864 iterations
...i=16, added 157216 iterations
...i=64, added 39304 iterations
...i=256, added 9826 iterations
...i=1024, added 2456 iterations
...i=4096, added 614 iterations
...i=16384, added 153 iterations
Total # of iterations for this test case: 3353889
Test #4:
n: 678
n^2: 459684, n^3: 311665752
...i=1, added 311665752 iterations
...i=4, added 77916438 iterations
...i=16, added 19479109 iterations
...i=64, added 4869777 iterations
...i=256, added 1217444 iterations
...i=1024, added 304361 iterations
...i=4096, added 76090 iterations
...i=16384, added 19022 iterations
...i=65536, added 4755 iterations
...i=262144, added 1188 iterations
Total # of iterations for this test case: 415553936
Test #5:
n: 2077
n^2: 4313929, n^3: 8960030533
...i=1, added 8960030533 iterations
...i=4, added 2240007633 iterations
...i=16, added 560001908 iterations
...i=64, added 140000477 iterations
...i=256, added 35000119 iterations
...i=1024, added 8750029 iterations
...i=4096, added 2187507 iterations
...i=16384, added 546876 iterations
...i=65536, added 136719 iterations
...i=262144, added 34179 iterations
...i=1048576, added 8544 iterations
...i=4194304, added 2136 iterations
Total # of iterations for this test case: 11946706660
Test #6:
n: 5601
n^2: 31371201, n^3: 175710096801
...i=1, added 175710096801 iterations
...i=4, added 43927524200 iterations
...i=16, added 10981881050 iterations
...i=64, added 2745470262 iterations
...i=256, added 686367565 iterations
...i=1024, added 171591891 iterations
...i=4096, added 42897972 iterations
...i=16384, added 10724493 iterations
...i=65536, added 2681123 iterations
...i=262144, added 670280 iterations
...i=1048576, added 167570 iterations
...i=4194304, added 41892 iterations
...i=16777216, added 10473 iterations
Total # of iterations for this test case: 234280125572
Test #7:
n: 11980
n^2: 143520400, n^3: 1719374392000
...i=1, added 1719374392000 iterations
...i=4, added 429843598000 iterations
...i=16, added 107460899500 iterations
...i=64, added 26865224875 iterations
...i=256, added 6716306218 iterations
...i=1024, added 1679076554 iterations
...i=4096, added 419769138 iterations
...i=16384, added 104942284 iterations
...i=65536, added 26235571 iterations
...i=262144, added 6558892 iterations
...i=1048576, added 1639723 iterations
...i=4194304, added 409930 iterations
...i=16777216, added 102482 iterations
...i=67108864, added 25620 iterations
Total # of iterations for this test case: 2292499180787