###

» Fri May 05, 2017 10:34 am

@skrotar

When you buy multiple of an item do you want the cost per item to remain the same for that multiple purchase or to compound ?

e.g. if it was my first purchase and I want to buy 10 then if the price remained the same per item the total cost would be 60 , but if it compounded it would be 6+9+13+19+... etc

Calculating the former is simple, it's just bulkCost = itemNo*itemCost

then you step up the item cost for the next purchase:

itemCost = floor(itemCost*(1+mulitplier))

To find the max purchase, if C is the number of coins then itemNo = floor(C/itemCost) and then you reapply that value to the bulkCost formula.

If, on the other hand, you want the cost to be compounded on bulk buys you could use the traditional compound interest formula. The formula is:

A = floor(P*(1+r)^t)

A = the bulk Cost, P is the starting cost, r is the rate of interest and t is the number of items purchased.

so in the above example the formula would give:

A = floor(6*(1+0.5)^10) = 345

However this will give you a different result than if you looped to calculate the bulk cost using:

Trigger once: Set bulkCost to itemCost

Repeat itemNo-1 times:

>>itemCost = floor(itemCost*(1+multiplier))

>>add ItemCost to bulkCost

...because you're flooring the itemCost on each iteration, rather than just flooring the final result. I'm guessing that this looping calculation is the one you're after.

Now, for the final part of your question: how to calculate the max purchase for compounds:

Let C be the total number of coins.

Set bulkCost = itemCost

Set itemNo = 1

Trigger once:

while bulkCost <= C:

>>add 1 to itemNo

>>itemCost = floor(itemCost*(1+multiplier))

>>add ItemCost to bulkCost

This while loop will continue until you've exceeded the number of coins, after which you will need to step down the itemNo by one to get the max purchase:

If bulkCost > C, Trigger once

>> deduct itemCost from bulkCost

>> deduct 1 from itemNo

I think that will work,haven't checked it yet though - I'll try and put together a capx later. I'm also fairly sure there's a more elegant solution than this - hopefully some kind soul will enlighten us both.