Maths/Programming recursion question

53 Views Asked by At

I know this is a programming question but it seems to be more on the mathematical side ( recursion ) I was hoping someone would be able to explain it to me since it will probably be on my exam tomorrow.

http://vvcap.net/db/kIcAQxI5nJGOdN_l18Zb.htp

From 1,1 i cannot do it since the answer is 0 and i seem to get 0, -1 since a becomes 0 and b becomes -1....

This would be an absolute massive help if someone could explain to me where i'm going wrong because it will be worth 10 marks in the exam!

1

There are 1 best solutions below

10
On

Okay, first you call f(1,1). In this case, you have to evaluate the else branch where you have return f(f(a-1,b-1),b-1). Since a=1 and b=1 you can replace these a-1 and b-1 with its actual value 0. This gives you call return f(f(0,0),0). Since there is no assignment of any variable, you can replace these values simultaneously. Now, you have to evaluate this return f(f(0,0),0) in two steps. First you replace f(0,0) by its return value 0. This gives you return f(0,0). Again, since f(0,0) -> 0 you have return 0.