while(n>x)
x*=x;
The correct answer is log(log(n)), I can see the log(n) since x^k>=n is when the while loop will stop. so I got to log(n), what am I missing?
P.S: it is given that x=2.
Let a be the original value of x, and assume a>1.
x >= n means
a**(2**k) >= n
2**k >= log(n)/log(a)
k >= log2(log(n)/log(a)) = log2(log(n))-log2(log(a))
Expand the loop (let x = 2
) and you'll see:
x = 2
x = 4 // 2 * 2
x = 16 // 4 * 4
x = 256 // 16 * 16
x = 65536 // 256 * 256
...
x = 2 ** (2 ** n) // you can prove this by induction
and so
n = log(log(x))
you'll be quite right with n = log(x)
estimation if you have x *= constant
body, e.g. for the constant == 2
we have
x = 2
x = 4
x = 8
...
x == 2 ** n
where n
is just
n = log(x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With