I am trying to find the running time O(n) for the block of code below:
int z=0;
int x=0;
for (int i=1; i<=n; i=i*3){ //runs from 1->n, 1, 3, 9, 27... <- fcn that defines this?
//constant running times below
z = z+5;
z++;
x = 2*x;
}
if it was i=i*2, then it would be logn complexity running time. What is it for this case?
tia.
Notice that log_3 (n) = log_2 (n) / log_2 (3) . Here log_2 means log with base 2 and log_3 means log with base 3. So, log_2(n) is just log_3(n) times a constant.
So, by big O definition, any code which has running time O(log_3(n)) is also O(log(n)).