1

I have a varied number and two fixed options to use to make up the total. For example with the value 360g with fixed values of 80g or 100g I would need.

Value    Amount

80g           2

100g          2

I need an algorithm to work out the best options. The other issue is that the options may not always fit exactly so the option with the least remainder is required. The one I have currently will work out a possible solution but not always the best.

Any suggestions would be great. Thanks

Sam Blake
  • 11
  • 1
  • What is your definition of "best". What is given in your problem. What the problem should provide you with. The question does not make that clear. – Jan Feb 06 '17 at 23:10
  • The best is the options that can make up the value but with minimal remainder. Example is chocolate. I need 360g and they sell 80 or 100g bars. Assuming the price per g is the same which is the cheapeast way to get 360. – Sam Blake Feb 06 '17 at 23:17
  • OK, can I write your problem as, given $a,b,c>0$, find solution to $\min_{i,j\in\mathbb{N}_{0}}|a-(b\cdot i+c\cdot j)|$? – Jan Feb 06 '17 at 23:43
  • 1
    Is this your question? Given arbitrary integers $n$, $n_1$ and $n_2$ (with $n > { n_1, n_2 }$), what algorithm finds integers $k_1$ and $k_2$ such that $| n - k_1 n_1 - k_2 n_2 |$ is minimal? It has nothing to do with grams, for example. – David G. Stork Feb 06 '17 at 23:44

1 Answers1

1

There are a number of closely-related questions here whose answers may help: Chicken Mcnugget Theorem (Frobenous Coin) Problem, Least wasteful use of stamps to achieve a given postage, and Using only postage stamps of value 64 and 55, how can I work out the way to get closest to a high parcel value?

Using the example of candy bars that come in two sizes by weight, $a$ grams or $b$ grams, clearly the only amounts that can be purchased exactly are multiples of $\gcd(a,b).$ If the amount we need is not a multiple of $\gcd(a,b),$ we'll have to buy enough "wasted" candy to bring the total weight up to a multiple of $\gcd(a,b).$

I would prefer to simplify the problem by treating $\gcd(a,b)$ grams as our unit of weight. For example, if bars are either $80$ grams or $100$ grams, then since $\gcd(80,100)=20$ I would like to introduce a unit of weight equal to $20$ grams, so we now have bars whose weights are either $4$ units or $5$ units.

This simplifies the problem not only because the numbers are smaller, but also because we just have to purchase an integer amount of candy, using the least integer which is greater than or equal to the amount (in our new units) that we wanted to buy, and it guarantees that the weights of the candies (in the new units) are relatively prime.

So in general, supposing we wanted to buy $x$ grams of candy consisting of bars of $a$ grams or $b$ grams, we write \begin{align} u &= \frac{a}{\gcd(a,b)}, \\ v &= \frac{b}{\gcd(a,b)}, \\ n &= \left\lceil\frac{x}{\gcd(a,b)}\right\rceil. \\ \end{align} As a result, $\gcd(u,v)=1,$ that is, $u$ and $v$ are relatively prime.

Now it is a well-known fact that if $n > uv - u - v,$ where $u$ and $v$ are relatively prime, then we can buy candy bars of weights $u$ and $v$ so that their total weight is $n$ exactly. By understanding the proof of this fact (in an answer to an earlier question), you should be able to devise an algorithm for finding the correct number of candy bars of each type to buy in that case.

If $n \leq uv - u - v$ then the proof does not work, and indeed some of those weights of candy may not be exactly achievable. But for two fixed weights $u$ and $v,$ you can easily make a table of the weights that are achievable and the number of bars of each type to buy to achieve each weight. Simply try each multiple of $u$ less than $uv - u - v,$ and for each multiple $ku$ try each multiple of $v$ less than $(uv - u - v) - ku.$ For weights that cannot be achieved exactly, you simply use the next greater weight that can be achieved.

David K
  • 108,155