Reputation: 79
I have this code and I have to know the time complexity of it, I am a beginner in this subject and it confuses me a little bit.
void func(int n){
while(n > 0 && n % 3 ==0){
do_computation(n);
n = n/3;
}
}
the function do_computatation have a time complexity of 1 then the worst case of func would be O(log3n) is that right ?
Upvotes: 2
Views: 923
Reputation: 99094
Yes, it's O(log3n), but that's the same as O(log n). The base doesn't matter, since logan = (logab)(logbn), and constant factors don't matter.
EDIT:
log(ab) = b log(a) (in any base)
n = blogbn
Take loga of both sides:
logan = loga(blogbn)
=(logbn)(logab)
Upvotes: 4