rian bron wrote:... 1-9 with a recursion call instead of copying it ...
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 ?
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 ?
rian bron wrote:...Yes the number must me between 1 to 9 (x>=1&&x<=9)
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 ?
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 ?
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 ?
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 ?
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.
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
|