Piet Souris wrote:I would start by writing a method 'int removeLastNode()' and 'int removeFirstNode()' in your IntListTwo class. The return value is the value of the removed node.
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.
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: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:
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:
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 ?
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 ?
Paul Clapham wrote:Well, first of all your code is very hard to read because you use the accessor methods instead of the instance variables. If I rewrite your code to remove that idea (and to remove the complicated method of squaring a number, and to remove unnecessary brackets) then it looks like this:
When I compare that to the quadratic formula I can see at least two differences, probably three. And that's before considering the possibility of complex numbers as the solution.