for i := 1 to n do
j := 2;
while j < i do
j := j^4;
I’m really confused when it comes to Big-O notation, so I’d like to know if it’s O(n log n). That’s my gut, but I can’t prove it. I know the while loop is probably faster than log n, but I don’t know by how much!
Edit: the caret denotes exponent.
The problem is the number of iterations the while loop is executed for a given
i.On every iteration
j:= j^4and at the beginningj := 2, so afterxiterationsj = 24^xj < iis equivalent tox < log_4(log_2(i))I’d risk a statement, that the complexity is
O(n * log_4(log_2(n)))You can get rid of constant factors in Big O notation.
log_4(x) = log(x) / log(4)andlog(4)is a constant. Similarly you can changelog_2(x)tolog(x). The complexity can be expressed asO(n*log(log(n)))