# Probably an easy math question

Get help using Construct 2

### » Fri May 05, 2017 8:02 am

Ok im sure this is simple and i'll feel really dumb when someone shows me but my brain is not working right now

I have a variable For currency, and a variable for an item cost, also every time this item is bought its price goes up by a variable which is a multiplier.

Example: Cost of an object is 6 and the multiplier (.5) is floor(cost + (cost*Multiplier). The first item bought is then 6 and the second one is then 9.

Now the part im having trouble with - I want to be able to buy multiple of that item at once. Lets say you can choose to buy 1, 10, 100 or max. Max would be based on their total coins how many they could buy of said object.

Thanks for any help
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

### » 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))

...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:
>>itemCost = floor(itemCost*(1+multiplier))

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.
B
23
S
9
G
4
Posts: 499
Reputation: 4,415

### » Fri May 05, 2017 3:35 pm

A capx would be greatly appreciated
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

### » Fri May 05, 2017 3:40 pm

Just to add, that is a geometric series and if you didn't have the floor there is a formula you could use to calculate the sum of any number of them without a loop:
https://www.varsitytutors.com/hotmath/h ... ric-series
B
94
S
33
G
128
Posts: 5,488
Reputation: 81,539

### » Thu Nov 16, 2017 3:25 pm

Message: DianeMarx can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
When I need https://doahomework.com/dissertation-writing-services/ writing the perfect thesis for a research paper, I usually use it! I think it can be used everyone and you can enjoy your free time!
Banned User
B
1
Posts: 1
Reputation: 127