The full question is
Supposed we have a function
void foo(int n){
int i = n;
while(i > 0){
//do an O(n) operation
//do some O(1) operations
i = sqrt(i) - 1;
}
}
I just have to find the asymptotic bounds, but I can’t do that until I figure out how many times the loop actually runs. Which I’m guessing is another summation involving a square root, but I’m not sure how to start.
You want to find you how many times the loop will execute.
If i < 2, then the loop will execute at most twice.
Therefore if i < 4 the loop will execute at most 3 times.
Therefore if i < 16 the loop will execute at most 4 times.
Therefore if i < 256 the loop will execute at most 5 times.
…
etc.
You see that if i < 2^(2^m), then the loop will execute at most (m+2) times.
This means that the order of the number of times it will execute is log(log(n)),
since i starts at n.
Thus the overall complexty is
O(n*log(log(n)).(That’s if the number of O(1) operations in each iteration is constant.)