- Local time
- Today, 02:08
- Joined
- Sep 12, 2006
- Messages
- 15,956
I was also going to say you should start from the largest sizes, not the smallest, with a recursive procedure. It's probably similar to packing algorithms, where you try to fill load spaces starting with the biggest items that need loading.The code is a loop so it doesn't matter how many sizes there are. For example, 250, 125, 50, 25. If you want a dose of 275, you end up with 250 + 25. It doesn't matter that 2(125), and 1(25) also satisfy or if 11(25) works. Starting from the large size, you always get the most efficient breakdown. The middle sizes are tried and discarded. The complications arise if you want to limit the number of pills and the available sizes can't satisfy the requirement. Or, you don't have inventory in the sizes that will work. The looping also is very complicated if the sizes are not multiples. For example if 37.5 and 25 were the only options a final solution could not be found without backing out and trying again which gets really complicated.
You can get to "True" from either direction assuming the available sizes provide a solution, but do you want to dispense 11 pills or 2 is the question.
I see @The_Doc_Man made similar points..
If you have pill sizes of 250, 100, 40, and want a dose of 120, though, you would find you try to use the 100, but have to reject that, before you get the correct solution of 3x40. I imagine that pill sizes and doses are sort of standardized so that doses can always be constructed.
Looking at the responses, I can also see you already have an impressive solution from @MajP
Last edited: