0

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 :)

1
  • Please explain more about your question.. Commented Apr 12, 2013 at 16:27

2 Answers 2

1

I wrote a basic brute-force style script in python to compare the prices of the two options (up to 1,000 elements). It's not the fastest nor most elegant approach but it appears to work.

The format on the output is: (<unit-count>, (<per-unit-cost>, <per-batch-cost>))

import math import itertools import pprint unitList = range(1000) pricePerUnit = .05 pricePerBatch = 4.0 numberPerBatch = 100.0 def calculatePerUnit(units): """ Calculate the price of buying per unit """ return units * pricePerUnit def calculatePerBatch(units): """ Calculate the price of buying per batch """ return math.ceil(units / numberPerBatch) * pricePerBatch def main(): """ Execute the script """ perUnit = map(calculatePerUnit, unitList) perBatch = map(calculatePerBatch, unitList) comparisonList = zip(perUnit, perBatch) perUnitCheaperPriceList = list(itertools.ifilter(lambda x: x[0] < x[1], comparisonList)) perUnitCheaperUnitList = map(lambda x: int(x[0] / .05), perUnitCheaperPriceList) pprint.pprint(zip(perUnitCheaperUnitList, perUnitCheaperPriceList)) if __name__=="__main__": main() 

And the results:

[gizmo@test ~]$ python TippingPoint.py [(1, (0.050000000000000003, 4.0)), ... These are sequential values I left out for brevity ... (79, (3.9500000000000002, 4.0)), (101, (5.0500000000000007, 8.0)), ... These are sequential values I left out for brevity ... (159, (7.9500000000000002, 8.0)), (201, (10.050000000000001, 12.0)), ... These are sequential values I left out for brevity ... (239, (11.950000000000001, 12.0)), (301, (15.050000000000001, 16.0)), ... These are sequential values I left out for brevity ... (319, (15.950000000000001, 16.0))] 
Sign up to request clarification or add additional context in comments.

Comments

1

The answer to this is astonishingly easy, if you just think of it the other way around. You already know the 'tipping point' in price. It is $8.00. What you do not know is that point in units, which is $8.00 divided by $0.05 (per units) => 160 units.

Please note that there also is the chance of buying 100 units @ $4.00 and 60 units @ $0.05, resulting in $7.00 total. That is a third possibility that you may or may not account for.

Edit: After your edit, things are getting even more simple:

you have 1 item at $XX, and a batch of NN items at $YY. Assuming that $YY/NN < $XX (meaning, the batch actually saves money), all you have to do is this:

  1. Divide your target quantity by NN, round that down. You will have to buy this number of batches
  2. Multiply the remaining quantity with $XX. If that is more than $YY just buy an extra batch, otherwise buy the remaining quantity at $XX

3 Comments

I'm not sure this is 100% correct. For instance, at 319 units the per unit option costs $15.95 and the per batch option costs $16. There may be multiple tipping points.
yes, there very well may be multiple tipping points, above example is the one between 100 and 200 units. The one between 200 and 300 is $12/0.05=240 and the last one is $16/0.05 = 320. There is none above that because for $20 you get an extra batch "for free" (500 vs. 400)
Thanks for the responses -- I have added some clarification to my original question - mixtures of units/batches is definitely allowed ... sorry for the unclear original post

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.