For each of the below exercises, first state the base condition. Secondly, perform (without a computer) a full trace showing the levels of recursion.
int f(int x)
{
if (x <= 2) return 1;
return f(x-1) + f(x-2);
}
Find f(7)
.
int f(int x, int y)
{
if (y == 0) return x;
if (x > y) return f(y, x);
return f(x, y % x);
}
What is an important precondition on x? f(27,9)
.f(45,5)
.f(36,17)
.
int f(int x)
{
if (x > 5)
return f(f(x - 2) - 1) + x;
if (0 < x && x <=5)
return f(f(x - 1) -2) + 2;
return x;
}
Find f(9)
.
int f(int x, int y)
{
if (x <= 0)
return y;
if (y <= 0)
return x;
if (x > y)
return f(y -1, x -1) + x*y;
return f(x, y/2);
}
Find f(4,3)
.