【发布时间】:2017-01-29 03:26:08
【问题描述】:
这是伪代码。正如this answer 所说,我试图计算这个函数的时间复杂度。应该是这样的:
n + n/3 + n/9 + ...
我猜可能时间复杂度类似于O(nlog(n))?或者log(n) 应该是log(n) base 3?有人说时间复杂度是O(n),我完全无法接受。
j = n
while j >= 1 {
for i = 1 to j {
x += 1
}
j /= 3
}
【问题讨论】:
-
几何级数相加即可。
-
@AbhishekBansal 就像这样
n + n/3 + n/9 + ...?但这不是 O(n)。
标签: algorithm function loops math time-complexity