Time and Space Complexity

Q14. 5. TIme and Space
Find the time complexity of the given code.

void pat(){

for (int i = 2; i <=n; i = pow(i, c)) {
//Print statement - O(1) operation
}

//Here fun is sqrt or cuberoot or any other constant root
for (int i = n; i > 1; i = fun(i)) {
//Print statement - O(1) operation
}

Why does the loop run logN times?

first iteration i is 2
in second iteration i will be 2^k
in third iteration i will be (2^k)^k = 2^(k^2)
in fourth iteration i will be 2^(k^3)

in i+1th iteration i will be 2^(k^(i))

for loop to stop
2^(k^(i) ) > n
take log base 2 both side
k^(i) > log2(n)
take log base k both side
i > log k log2(n)
so this option is correct->LogLog N

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.