• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

i cant find the logic in this recurtion

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how does it work??

Public static void partitions (int n)
{
partitions (n, n, "");
}
Public static void partitions (int n, int max, String prefix)
{
if(n==0)
{
System.out.println(prefix);
}
else
{
for(int i= Math.min(n,max); i>=1, i--)
{
partitions (n-i, i, prefix+" "+i);
}
}
}
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags, which make code easier to read. Please use ctrl-C and ctrl-V rather than writing code out; this will save you from little mistakes like "Public" for "public".

You have a base case, though I think maybe it would work better with 1 than with 0.

You have a reduction step.

You are calling your own method.

You will have to go through the whole method with a pencil and paper and work out what is happening. You will get something like this:
  • You go into the partition(int) method
  • This calls the partitions(int, int, String) method
  • You go into the partitions(int, int, String) method
  • It starts by seeing whether n is zero.
  • If it is zero it . . . .
  • etc etc.
  • As an alternative, see if you can get that method to run, get an IDE and debug it with the step into, step over, and step return commands. Do it several times and inspect the variables and the method call stack (which is usually displayed somewhere) and watch what happens. I told you how to do that about a week ago.
     
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As an easy alternative, you can easily put a meaningful System.out.println() as and when required, if debugging in an IDE mentioned by Campbell sounds too big for you.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Raghavan Muthu:
    As an easy alternative . . .

    Good idea
     
    reply
      Bookmark Topic Watch Topic
    • New Topic