Difference between exponential and logarithmic growth

algorithmscomplexity

Why

for(k=1;k<=n;k*=2) grows logarathmically = O(logn)
but I feel it grows exponentially, as the seq look like 1,2,4,8....

and for fibonacci series people say it grows exponentially.
which for me doesnt look like 0,1,1,2,3,5...
but for tis they tell O(2^n).

PLease explain

Best Answer

You're misunderstanding the meaning. They are probably talking about the complexity of an algorithm, which got nothing to do with the resulting sequence of numbers (which you seem to talk about).

Just look at some snippets.

Compare for example for(k=1;k<=5000;k*=2) with for(k=1;k<=10000;k*=2). You'll see that the second will only take 1 more iteration. In fact doubling n will always take only one step more.

Now lets look at the algorithm for the fibonacci sequence that was probably being talked about:

int fibonacci(int n)  {
    if(n <= 1) return n;
    else return fibonacci(n - 1) + fibonacci(n - 2);
}

Now lets look at how often the function gets executed for several values for n:

n = 0:  1    function call
n = 1:  1   function call
n = 2:  3   function calls
n = 3:  5   function calls
n = 4:  9   function calls
n = 5:  15  function calls
n = 6:  25  function calls
n = 7:  41  function calls
n = 8:  67  function calls
n = 9:  109 function calls
n = 10: 177 function calls

In fact you got the one function call you make when calling fibonacci(n) + the amount of function calls being made in fibonacci(n-1) + the amount of function calls in fibonacci(n-2).
Therefore increasing n just slightly would make an huge increase in the number of function calls actually being made.

Related Topic