Code optimization of recursive function I have a mathematical function as shown below


  $$ (h+1) * U(k,h) = \sum_{r=0}^k \sum_{s=0}^h (r + 1) * (k - r + 1) *
 U(r + 1, h - s) * U(k - r + 1, s)\ + \sum_{r=0}^k \sum_{s=0}^h (k - r
  + 1) * (k - r + 2) * U(r, h - s) * U(k - r + 2, s)\,. $$


Base condition for U(k,h) is


  $$ U(k,0) = (-1)^k/k! $$


I am using the following code to calculate the function


public double U(int k, int h)


        {
            h--;
            Int16 r, s;


            if (h == -1)
            {
                return (double)(Power(-1, k) / fact[k]);
            }

            else if (arr1[k, h] != 0 &amp;&amp; arr2[k, h] != 0)
                return (double)((arr1[k, h] + arr2[k, h]) / (h + 1));


            else
            {
                for (r = 0; r &lt;= k; r++)
                {
                    for (s = 0; s &lt;= h; s++)
                    {
                        arr1[k,h] += (r + 1) * (k - r + 1) * U(r + 1, h - s) * U(k - r + 1, s);

                    }

                }
                for (r = 0; r &lt;= k; r++)
                {
                    for (s = 0; s &lt;= h; s++)
                    {
                        arr2[k,h] += (k - r + 1) * (k - r + 2) * U(r, h - s) * U(k - r + 2, s);
                    }
                }
                return (double)((arr1[k,h] + arr2[k,h]) / (h + 1));
            }
        }



The function works fine for values of k and h up to 40. After that it prints            


  at ndp.functions.U(Int32 k, Int32 h)
Base case first

It really helps to get a clear idea of a recursive function when the base case is the first thing you see. In your code it is even worse, as h is mutated before the base case, so it looks like the base case is -1 and not 0.

Smallest scope possible

Declare loop counters inside the for loop statement. It is more efficient and reduces mental load when reading the code.