I have the following code snippet and I want to know the time complexity of running it:
func fun(int n)
{
var j = 1, i = 0;
while (i < n)
{
// Some O(1) task
i = i + log(j);
j++;
}
}
After some calculation, it comes up that the time complexity should be some kind of the form $c e^{W(n)} + ...=O(e^{W(n)})$, where $c$ is a constant and $W(n)$ is the product log function. I know the result in the above statement should be wrong but I don't know how to correct it.
Any correction and help would be appreciated.
This is actually quite simple. From the other answer it seems that you know that if $L$ is the number of iterations, then $n \sim L \log L$. We can safely take the log of both sides (you should think about why this is justified, especially because it is not true in reverse) to obtain $\log n \sim \log L + \log \log L$.
But since $\log \log L = o(\log L)$, the RHS is also asymptotic to $\log L$, i.e., $\log n \sim \log L$. (In other words, $\log n$ and $\log L$ are different, but not by much.)
Thus, $n \sim L \log L \sim L \log n$, which rearranges to $L \sim n/\log n$.