Friday 15 July 2011

algorithm - I am confused about Recursion in power function -


The following is the code of power (mathematical).

  1. I am confused, it seems that every problem is divided into a subproblem, each of the size of each, so it is not making a tree, because usually a recursive "tree "You need two recurring calls for. This is something like a simple list with only a recurring call. But this is a recursive function, factorial and many other recursive functions forms, and they have the same recycling. 2.If it is producing a tree, is it transmitting all the paths or the single path?
      public int GetPower (int, int n) {if (n == 0) {return 1; } Else {int t = GetPower (k, n / 2); If ((n% 2) == 0) {return t * t; } Else {return k * t * t; }}}   

    Please help me, my confusion needs some explanation.

    Edit

      (2,20) - & gt; (2,10) - & gt; (2,5) - & gt; (2,2) - & gt; (2,1) - & gt; (2,0) 1048576 & lt; - 1024 & lt; 32 & lt; 2 ^ 4 * 2 & lt; - 2 * 2 & lt; - 2 & lt;       If you want to calculate, you want answer 2 ^ 6. If you are happy you have been given answer 2 to 2 ^ 3. You will now multiply only  2 ^ 3 * 2 ^ 3  = 8 * 8 = 64.  

    It is the argument that is used.

    For Strange Powers Like:

    2 ^ 5

    We calculate the answer of 2 ^ 2 and do it :

    2 * 2 ^ 2 * 2 ^ 2

    Beautiful simple move, but change the complexity of time from O (N) to O (Log N) where N is power.

No comments:

Post a Comment