Results 1 to 2 of 2
  1. #1
    gmnnn is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    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?
    Last edited by wtd_nielsen; 12-06-2009 at 04:28 PM.

Similar Threads

  1. Recursion depth in java
    By poulius in forum New To Java
    Replies: 17
    Last Post: 01-11-2009, 01:38 PM
  2. java recursion help
    By khdani in forum New To Java
    Replies: 4
    Last Post: 12-29-2008, 08:56 PM
  3. Recursion in Java ..
    By Java01 in forum New To Java
    Replies: 6
    Last Post: 10-24-2008, 11:42 AM
  4. i could not get the recursion in java
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 04-23-2008, 08:08 AM
  5. Recursion in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •