• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Help with writing this recursion backtracking algorithm

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys, i am stuck a few days already on my problem my school gave me:
The question is :
"Write a recursive method without using any loops, that will  count all the number of possible compositions of x1+x2+x3=num."
i tried to do it with the following code:


I have 2 problems:
1.The method needs to return  an int that represents the number of results possible, but of some reason the returned is always 1, why is that?

2. lines 15-32 makes the code really go longer than it should, is there a  way to get all over the number 1-9 with a recursion call instead of copying it over and over again?
Thank you Very much!
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are negative numbers allowed ?
Then I think there could be infinite possibilities.

Instead of trying to solve this directly in Java code, can you work it out in plain english, how would you manually solve this problem.
e.g. Lets take num=5
what are the possibilities of x1, x2, x3 that add up to 5 and how did you come up with that ?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rian bron wrote:... 1-9 with a recursion call instead of copying it ...


Why is 0 not considered ?
e.g. if num=5, then 0+0+5 =5

Are there any other restrictions on the value of x1, x2, x3 ?
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:Are negative numbers allowed ?
Then I think there could be infinite possibilities.

Instead of trying to solve this directly in Java code, can you work it out in plain english, how would you manually solve this problem.
e.g. Lets take num=5
what are the possibilities of x1, x2, x3 that add up to 5 and how did you come up with that ?


Sorry i forgot to mentioned that negative numbers are not allowed,
and I tried to do what you said, and this is the result i got into.
The main problem is that the returned int is always staying 1 and does not represent the real answer.
It is probably because i don't insert the counting in the correct place, but i tried few places and still didn't get it right.
Can you help me with that?
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:

rian bron wrote:... 1-9 with a recursion call instead of copying it ...


Why is 0 not considered ?
e.g. if num=5, then 0+0+5 =5

Are there any other restrictions on the value of x1, x2, x3 ?


Yes the number must me between 1 to 9 (x>=1&&x<=9)
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rian bron wrote:...Yes the number must me between 1 to 9 (x>=1&&x<=9)



Great, Now that we know that, I hope there are no other restrictions such as repeating values for x1,x2,x3.
As I asked before, can you work out in plain english and not in java code, how you would solve this problem ?
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:

rian bron wrote:...Yes the number must me between 1 to 9 (x>=1&&x<=9)



Great, Now that we know that, I hope there are no other restrictions such as repeating values for x1,x2,x3.
As I asked before, can you work out in plain english and not in java code, how you would solve this problem ?



Lets say i am working right now on X,  so as a start x,y,z=1.
and i will add up 1 each round until x will be equals to 10, when this will happen I will set x=1 and start add up 1 to y each round until y=10. when this will happen i will set y=1 and add up 1 each round to z.
when z will be equals to 10, i will return the counting variable?


Is this method is correct?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unsure why you are counting till 10, but here's my approach:

First of all, we need to validate num. Since you are allowing x1,x2,x3 to be a min of 1 and a max of 9, num can only lie between 3 and 27.

The simplest brute force to check a number (say 5) would be to loop between 1 to 3. (i.e. 1 till num-2)




Do you see a pattern emerging above ?
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:I am unsure why you are counting till 10, but here's my approach:

First of all, we need to validate num. Since you are allowing x1,x2,x3 to be a min of 1 and a max of 9, num can only lie between 3 and 27.

The simplest brute force to check a number (say 5) would be to loop between 1 to 3. (i.e. 1 till num-2)




Do you see a pattern emerging above ?



you are basically right, but the thing is the i need to get all the possible compositions for the number.
for example if you chose 5, i need to get 6 as an answer:
1 1 3
1 2 2
1 3 1
2 1 2
2 2 1
3 1 1
It doesn't only need to find the possible compositions from left to right, it needs to find all compositions from right to left too.
But my problem is with the returning int, i get a wrong int every time i run this method, i get 1 every time instead of the correct number of results possible.
can you help me with that?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example I showed you for only for x2,x3 for x1=1. It gets 3 matches. Similarly you can loop for x1=2,3,etc..

Lets forget recursion, can you implement this using a simple for loop ?
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:The example I showed you for only for x2,x3 for x1=1. It gets 3 matches. Similarly you can loop for x1=2,3,etc..

Lets forget recursion, can you implement this using a simple for loop ?



yes, i would do it like this:

 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:The example I showed you for only for x2,x3 for x1=1. It gets 3 matches. Similarly you can loop for x1=2,3,etc..

Lets forget recursion, can you implement this using a simple for loop ?



Ok! i figured it out how to write the same method with recursion, but i now have my last problem, the method i wrote gives me all the results compositions possible , but the method does not stop running, i think it is  a problem with the base case but i don't know what:



Do you know what is the problem?
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of using if, use else-if since your cases are exclusive. I think there is some more problems with the counts anyways.
When using recursion, I think its best to print all variables to see what is happening in each call.
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:instead of using if, use else-if since your cases are exclusive. I think there is some more problems with the counts anyways.
When using recursion, I think its best to print all variables to see what is happening in each call.


you are right, but my task is to print the number of results there are , for example for num=4, i need to get 3 because there are 3 results possible: 1 1 2, 1 2 1 , 2 1 1
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what you need, here's your code with a little modification from my end:

 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This ensures that for smaller numbers, the method does not iterate through unnecessary values. Like I mentioned before, your code simply needed an if-else instead of just a if statement.
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic