Java Recursion Problem

• 11-27-2009, 03:32 PM
gmnnn
Java Recursion Problem
Hey guys,
I'm gonna write that code:
There is the duo P(a,b) and my code finds how many ways are there to represent "a" as sum of "b" number
Example: P(4,2)
4=3+1 ve 2+2. If there was P(4,3), it would be only 1+1+2 etc.
We have those infos:
for P(a,b), if a=b, there is 1 way
if a<b, there is no way and
for b=2, there are "the floor of a/b" way. Example: for P(7,2), (7/2)=(3,5)=3 different ways.
In addition, we know that P(a,b) = P(a-1,b-1)+P(a-b,b). The purpose is to reduce given a,b duo to duos which we know, by applying the last operation that i wrote. Example:
P(9,4) = P(8,3) + P(5,4)
= P(7,2) + P(5,3) + P(4,3) + P(1,4)
We know that how many ways are there to represent P(7,2) duo (3) and P(1,4) duo can be written as 0 different ways. We are trying to find the solution by applying same operation to P(5,3) and P(4,3) duos. But i couldn't write that code, show me a way please.
• 12-06-2009, 05:22 PM
wtd_nielsen
have you written any code that we could have a look at?
*edit:
hmm since its been a while since he asked the question, I will try with an solution:
public static int duo(int a, int b)
{
int result = 0;
if(a==b)
return 1;
else if(b==2)
{
return a/b;
}
else if(a>b)
{
result = duo(a-1,b-1)+duo(a-b,b);
}
return result;
}

but i dont think it is correct?