I'm struggling to find the time complexity of this function:
void foo(int n) {
int i, m = 1;
for (i = 0; i < n; i++) {
m *= n; // (m = n^n) ??
}
while (m > 1) {
m /= 3;
}
}
Well, the first for iteration is clearly O(n^n)
, the explanation to it is because m started with value 1
, and multiplies itself n
times.
Now, we start the while loop with m = n^n
and we divide it every time by 3
.
which means, (I guess), log(n^n)
.
Assuming I got it right up till now, I'm not sure if I need to sum or multiply, but my logic says I need to sum them, because they are 'odd' to each other.
So my assumption is: O(n^n) + O(log(n^n)) = O(n^n)
Because if n is quite big, we can just refrain from O(log(n^n))
.
Well, I really made many assumptions here, and I hope that makes sense. I'd love to hear your opinions about the time complexity of this function.