15,945,603 members
1.00/5 (1 vote)
See more:
Need help
Write a method for this in java:-
A merchant has a 40 kg weight which he used in his shop. Once, it fell from his hands and was broken into 5 pieces. But surprisingly, now he can weigh any weight between 1 kg to 40 kg with the combination of these 5 pieces.

What I have tried:

```We know a*A + b*B + c*C + d*D = x for all x between 0 and 40, and a, b, c, d are confined to -1, 0, 1. Clearly A + B + C + D = 40. The next case is x = 39, so clearly the smallest move is to remove an element (it is the only possible move that could result in successfully balancing against 39):

A + B + C = 39, so D = 1, by neccessity.

next:

A + B + C - D = 38

next:

A + B + D = 37, so C = 3

then:

A + B = 36

then:

A + B - D = 35

A + B - C + D = 34

A + B - C = 33

A + B - C - D = 32

A + C + D = 31, so A = 9

Therefore B = 27

So the weights are 1, 3, 9, 27

Really this can be deduced immediately from the fact that they must all be multiples of 3.```
Posted
Updated 3-Jun-22 11:38am
Dave Kreskowiak 2-Jun-22 18:06pm
You never asked a question or stated what part of this you're having a problem with.
FreedMalloc 2-Jun-22 22:42pm
The problem statement said the weight broke into 5 pieces.
Doesn't that mean A + B + C + D + E = 40?
Mritunjai Sharma 2-Jun-22 23:24pm
Yes

Solution 1

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

Solution 2

What follows below you may already know and if so I apologize. I was unsure from your post. This was an interesting math problem to me so I spent some time noodling it over. I hope this can help you.

In your write up you state: `a*A + b*B + c*C + d*D = x; for all x between 0 and 40; where a, b, c & d are constrained to -1, 0, or 1`. You run thru your calculations to find the values for A, B, C & D, You then state: Really this can be deduced immediately from the fact that they must all be multiples of 3. I don’t know how this was deduced, I certainly couldn’t. But, I did notice that the weights are not just multiples of 3, they are powers of 3. Specifically 3^0, 3^1, 3^2 & 3^3, where "^" indicates raising to that exponential power.

In fact, a more general case for this algorithm is: `c0*3^0 +c1*3^1 + c2*3^2 + c3*3^3 + ... + cn*3^n = x; for all x between 0 and T; where each value of c is constrained to -1, 0 or 1 and T is the sum where all values of c are 1.` In your example n =3 and T is 40. But if n = 4 then T will be 121.

I don’t know why this works but it does – at least as far as I took it. There is probably some mathematical proof for it laying around somewhere but if not then I leave it as an exercise for whoever reads this. It sounds like a candidate for an inductive proof but those days are 40 years behind me and have been liberally scrubbed with alcohol.

The problem statement says the weight was broken into 5 pieces. The algorithm above will still work, you just further break the 27 kg piece into 26 kg & 1 kg, but treat the 2 pieces as 1 by always adding them together first. If 6 pieces then 25, 1, and 1; and so on. If the original weight was greater than 40 (but less than 121) then the 5th piece would be W – 40. For example: if the original weight was 60 kg that broke into 5 pieces then the weight of the pieces would be 1, 3, 9, 27 and 20 kgs. If the original weight had broken into fewer than 4 pieces this algorithm will not work. I’m not sure where you go from there, but since that isn’t the stated problem perhaps you don’t need to care.

Where I’m going with all this is you need to sum powers of 3 from 0 to n until the sum is greater than the original weight, then set n to n-1. In your case; n = 4 would sum to 121 so n is 3 (which sums to 40). This is an iterative process – a loop in other words.

If you need to display how each term yields the various weights 0 – 40 kgs you will need to find the values of c for each term for each weight. For example: to get a weight of 1 the values of c would be 1, 0, 0, 0; a weight of 2 would be -1, 1, 0 , 0. You’ll need to find the pattern that yields each individual weight. That pattern will probably also be iterative.

I am hoping that this will help you visualize somewhat how you’ll need to code the final solution. I think I can see how I would do it, but I’ll leave that for you since this is your project to complete after all. I wish you the best of luck!

v2