dYTe
dYTe

Reputation: 301

Big O notation of this function

function A(n):
    if n ≤ 10 then
        return 1 fi;
    x := 0;
    for i = 1 to n do
        x := x + 1 od;
    return x * A(n/3) * A(n/6) * A(n/4)

My first idea was, that every call of A(n/c) is in O(log n) and since each has a for-loop from 1 to n it should be O(n log n). But since each call of A() also evokes 3 more it should also be somewhat exponential, right?

Upvotes: 2

Views: 66

Answers (1)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39400

The calculation of x simply assigns n to it with n steps. So we can assume the loop is just a dummy n steps.

The rest of the function can be brought down to:

return n * (A(n/3) ** 3);

In every recursive step, A is divided by 3. This means we effectively get a sum of n + n/3 + n/9 + ... until n/3 reaches < 0.5.

The whole thing needs to be multiplied by 3, but this itself won't change anything complexity-wise. Now such a sum (E(i = 0, inf) of n/(k^i)) converges to n/k-1, which is O(n) given a constant k. Of course doing actual divisions by 6 or 4 won't change anything either.

So the complexity of your entire function is O(n).

Upvotes: 2

Related Questions