# Thread: what this function does..

1. ## what this function does..

Code:
```int what(int n,int  m)
{
if(!n && !m) return 0;
if (n>m) return n + what (n-1,m);
return what (n,m-1)-m;
}```
if i go n=10 m=5
for the first if i get
return 10+9+8+7+6 +what(5,5) which is
return 10+9+8+7+6 +what(5,4)-5
return 10+9+8+7+6 +5+what(4,4)-5

what is the law??

2. i cant see the pattern in the output

3. what will be the output of

what(5,0)

what(5,1)

what(5,2)

what(5,3)

...

4. that takes too much time
is there any simpler way?

5. use iteration instead of recursion if you want speed, as far as simplicity goes, its simple enough

6. what??

7. use for or a while loop instead of call the function over and over again

8. What transgalatic is doing, is saying that it takes too much time to calculate the output in an exam, where everything is time-critical.

Try it for the cases of (1,1), (1, 2), (2, 1), (2, 2), (3, 1), (1, 3), (3, 2), (2, 3), (3, 3). Not very many cases, but it should give you a general idea of what the function does.

9. (1,1) = 0
(1,0) = 1
(2,2)= 1

i cant see a pattern

10. Originally Posted by transgalactic2
(1,1) = 0
(1,0) = 1
(2,2)= 1

i cant see a pattern
That's probably true. I would wager that no one would be able to find a pattern just from those three values. If you can't find a pattern from the first three values, keep going.

11. i dont have a sistematic way..

i am shooting in the dark
i need a sistematic way for picking input

12. Even if you don't know any numbers other than 0, 1, and 2, there are still six more input pairs that just use those numbers. (That is to say, you're going to need to know what happens everywhere, so look everywhere.) If you ever figure out any numbers other than 0, 1, and 2, then you can add that to your repertoire and make more pairs.

13. i dont have a sistematic way..

i am shooting in the dark
i need a sistematic way for picking input in order to get a formula

14. I'm not sure why you don't recognize "pick all the small inputs" as a system.

If you did look at the code at all, then you would have noticed that there are two different modes of moving -- decrease the first number if you're "below the diagonal" (i.e., if the first is bigger than the second), decreasing the second number if you're on or above the diagonal. So you should be able to see that you'll need to pick pairs in all three places (below the diagonal, on the diagonal, and above the diagonal).