I am a complete algorithm dunce and I have this problem where I need to find the lowest cost of buying an unknown quantity of widgets either by unit price or by buying in batches of x widgets... an example will help I'm sure:-
1) Widget price per unit is $0.05 2) Batches of widgets are priced at $4.00 per 100 widgets
Say I wish to buy 140 widgets -
a) costing it by unit is 140 x $0.05c => $7.00 b) costing it by batch is 2 batches of 100 @ $4.00 => $8.00 (the excess 60 widgets can be ignored)
so buying by unit in this case is cheaper by $1.00
However if I wish to buy 190 widgets then -
a) costing it by unit is 190 x $0.05c => $9.50 b) costing it by batch is 2 batches of 100 @ $4.00 => $8.00 (the excess 10 widgets can be ignored)
The price is cheaper in this case by batch buying...
So what I need to find out how to programatically find out where is the 'tipping point' between the 2 methods to get the cheapest price.
I hope I have explained it OK and I'm sure it's a simple answer but my brain has faded fast today!
TIA
EDIT::
Ok sorry -- I realise I haven't been as clear as I should -- as someone has pointed out a mix of batches and units is also possible so for the 140 widget example it could also be 1 batch and 40 units.
What I am trying to achieve is to programatically find the cheapest way to purchase X number of widgets priced each at $XX and also given a batch price $YY of NN widgets.
Any excess widgets from buying a batch is no problem i.e. it can be more than X purchased but it cannot be less than X
So for the 140 example 1 batch @ $4.00 + 40 units @ $0.05 => $6.00 which is the cheapest I think. And for the 190 example 2 batches is still the cheapest I think as 1 batch + 90 units is $8.50...
I was hoping there would be some neat equation out there which would do this :)