zombieFox / toaster

the toast will sustain
9 stars 2 forks source link

Calculation is wrong on buying multiple processor power #7

Open SouDescolado opened 5 years ago

SouDescolado commented 5 years ago

Each time you buy processor power, it changes the cost of the next one. However, if you buy multiple, it doesn't consider the increase between them

For example(Not the actual numbers): Lets say the increase is 1 per each buy. If the cost is 1, and I buy 1, I "pay" 1 toast and then next cost then would be 2. However, if the cost is 1 and I buy 10, I pay 10 toast ant the following cost would be 11. The correct cost would be 55 (1+2+3+4+5+6+7+8+9+10).

Will try to find the math formula that does this.

SouDescolado commented 5 years ago

Found it!

You need the starting number, the multiplier and the increment. S = Starting number M = How many are you going to buy I = How much it increments per buy

(S+M-1) x (S+M)/2 x I - (S-1) x S/2 x I = Total cost

If you want 10 multiplier, with 1 increment starting from 1 (1+10-1) x (1+10)/2 x 1 - (1-1) x 1/2 x 1 10 x 11/2 - 0/2 110/2-0 55

You are now at 11. Lets buy 10 more (11+10-1) x (11+10)/2 x 1 - (11-1) x 11/2 x 1 20 x 21/2 - 10 x 11/2 420/2-110/2 155

And that's it :)

zombieFox commented 5 years ago

Hi, thanks for providing this, really appreciate it. I've spent time this past week looking at the problem.

Lets start with the status quo:

However this is a brute force approach, calculating each step one by one. So this loop is not efficient at all. Running the loop for large numbers seriously slows the application down. So the formula you provided has been very useful. It does indeed work when calculating the cost of large numbers.

This is how I've currently set up your formula on a test branch (still in development):

target = the current amount of processor power
amount = amount of units to buy;
inflation = the increase in cost after each unit is bought
cost total = (target + amount - 1) * (target + amount) / 2 * inflation - (target - 1) * target / 2 * inflation;

I'm still in the works on implementing this formula, (thanks again for it). It would really help me out if you can add more details around the break down you added in the second comment. Could you explain the details of what each part of the formula is doing please?

Cheers dude 👍

SouDescolado commented 5 years ago

I had a problem with buying processor power. For example, now my cost is 6.213.800. If I try to buy 100, it returns 'toast inventory low, 621.380.000 toast matter needed#', which is the current cost x 100.

The first part of the example I mentioned is from a step you still have no increased cost. So

  1. (S+M-1) x (S+M)/2 x I <- shows the total cost, from zero to the number you want.
    • (S-1) x S/2 x I <- removes what you already paid for it

It works like this: You want to buy 100, so it applies the first formula, so it gives you the cost from zero to 100. However you already have 50, so it gives the cost from 0 to 50. Then you get the cost to 100 and removes the cost to 50, resulting in the difference, which is what you have to pay for it.

This type of increase is called triangular number. I didn't know it before. Learned it because of the toast!