The outer loop will be iterated n
times. The inner loop each time squared the previous value, i.e., 2 + 2^2 + 2^4 + 2^8 + ... + 2^{2^k}
. Hence, the time complexity is n * k
. To compute the k
, we need to find a k
such that n = 2 + 2^2 + 2^4 + 2^8 + ... + 2^{2^k}
:
2 + 2^2 + 2^4 + 2^8 + ... + 2^{2^k} = sum_{t=0}^{k} 2^{2^t} = n
=> k = heta(log(log(n))
Therefore, the time complexity if Theta(n log(log(n)))
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…