Probably an easy math question

0 favourites
  • 4 posts
From the Asset Store
Logo Quiz with 150 prepared logos and answers, automatic question and answer mixing
  • 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

  • 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.

  • A capx would be greatly appreciated

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)