简单的 while 循环 Big-O 复杂性
int a = 3;
while (a <= n) {
a = a * a;
}
我的版本是它的复杂性是:
http://www.mmoprophet.com /stuff/big-o.jpg
有这样的事吗?
int a = 3;
while (a <= n) {
a = a * a;
}
My version is that its complexity is:
http://www.mmoprophet.com/stuff/big-o.jpg
Is there such a thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那是不对的。 a 不能成为 big-O 公式的一部分,因为它只是一个临时变量。
在我的脑海中,如果我们将乘法视为常数时间运算,那么执行的乘法次数将是 O(log log n)。如果每次迭代都乘以一个常数,那么它将是 O(log n)。因为每次迭代都要乘以越来越大的数字,所以会有另一个日志。
将其视为每次迭代的位数加倍。在超出限制之前,您可以将位数加倍多少次?位数为 log n,您可以将起始数字加倍 log2 log n 次。
至于问题的另一方面,是的,O(a-n的根)可能是某些常量a的有效复杂性类>。更常见的写法是 O(n1/a)。
That's not right. a can't be a part of the big-O formula since it's just a temporary variable.
Off the top of my head, if we take multiplication to be a constant-time operation, then the number of multiplications performed will be O(log log n). If you were multiplying by a constant every iteration then it would be O(log n). Because you're multiplying by a growing number each iteration then there's another log.
Think of it as the number of digits doubling each iteration. How many times can you double the number of digits before you exceed the limit? The number of digits is log n and you can double the starting digits log2 log n times.
As for the other aspect of the question, yes, O(a-th root of n) could be a valid complexity class for some constant a. It would more familiarly be written as O(n1/a).
好吧,你实际上可以进入无限循环!
假设 32 位整数:
试试这个:
但是假设没有整数溢出,其他海报是正确的,如果假设 O(1) 乘法,则它是 O(log logn)。
一个简单的方法是:
xn+1 = xn2。
取 x1 = a。
记录日志。
tn = log xn
然后
tn+1 = 2tn
剩下的就交给你了。
如果考虑两个 k 位数字相乘的复杂性,事情会变得更有趣。
Well, you could actually go into an infinite loop!
Assume 32 bit integers:
Try this:
But assuming there are no integer overflows about, other posters are right, it is O(log logn) if you assume O(1) multiplication.
An easy way to see this is:
xn+1 = xn2.
Take x1 = a.
Taking logs.
tn = log xn
Then
tn+1 = 2tn
I will leave the rest to you.
It becomes more interesting if you consider the complexity of multiplication of two k digits numbers.
循环迭代次数为Ο(log log n)。循环体本身执行赋值(我们可以将其视为常量)和乘法。迄今为止最著名的乘法算法的步长复杂度为 Ο(n log n×2Ο(log* n)),因此,总而言之,复杂度是喜欢:
更易读的形式:
Ο(log log n × n log n×2^Ο(log* n)) http://TeXHub.Com/b/TyhcbG9nXGx vZyBuXCxcdGltZXNcLG4gXGxvZyBuXCxcdGltZXNcLDJee08oXGxvZ14qIG4pfSk=
The number of loop iterations is Ο(log log n). The loop body itself does an assignment (which we can consider to be constant) and a multiplication. The best known multiplication algorithm so far has a step complexity of Ο(n log n×2Ο(log* n)), so, all together, the complexity is something like:
In more readable form:
Ο(log log n × n log n×2^Ο(log* n)) http://TeXHub.Com/b/TyhcbG9nXGxvZyBuXCxcdGltZXNcLG4gXGxvZyBuXCxcdGltZXNcLDJee08oXGxvZ14qIG4pfSk=
第 i 次迭代 (i=0,1,...) 后,a 的值为 32i。将有 O(log log n) 次迭代,假设算术运算的时间复杂度为 O(1)。
After i-th iteration (i=0,1,...) value of a is 32i. There will be O(log log n) iterations, and assuming arithmetic operations in O(1) this is time complexity.