I am having a recursive function in C as shown below :
float U(int k, int h)
{
h --;
int r, s;
float sum1 = 0, sum2 = 0;
if (h == -1)
{
return (float) ((pow(-1, k)) / factorial(k));
}
else
{
for (r = 0; r <= k; r++)
{
for (s = 0; s <= h; s++)
{
sum1 += (r + 1) * (k - r + 1) * U(r + 1, h - s) * U(k - r + 1, s);
}
}
for (r = 0; r <= k; r++)
{
for (s = 0; s <= h; s++)
{
sum2 += (k - r + 1) * (k - r + 2) * U(r, h - s) * U(k - r + 2, s);
}
}
return (float) ((sum1 + sum2) / (h + 1));
}
}
I wish to optimize the code as the function takes a lot of time to calculate even not-so-large values like U(10,13). Please give some suggestions along with code snippets(if possible).