Search code examples
javaalgorithmcomplexity-theorycode-complexity

complexity of simple algorithm


I have the following algorithm but I dont know its' complexity. Could someone help me? Input size is n.

int x = n;
while (x > 0)
{
    System.out.println("Value is" + x);
    x = x/5;
}

Thanks a lot!


Solution

  • In each iteration x is divided by 5. How many iterations would it take for x to become lower than 1 (and therefore 0)?

    The answer is log5(n) (logarithm to the base 5 of n), which is O(log(n)).