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!

Doesn't that mean A + B + C + D + E = 40?