Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculation is wrong on buying multiple processor power #7

Open
SouDescolado opened this issue Oct 25, 2018 · 3 comments
Open

Calculation is wrong on buying multiple processor power #7

SouDescolado opened this issue Oct 25, 2018 · 3 comments

Comments

@SouDescolado
Copy link

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
Copy link
Author

SouDescolado commented Oct 25, 2018

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
Copy link
Owner

zombieFox commented Nov 3, 2018

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:

  • The loop I use to calculate the cost of inflating units for a given amount is in toaster.js line 681
  • It does work correctly as far as I can see, eg: buying processor power with a starting power of 1, starting cost of 8 and an increase in cost of 8 for each next unit, it produces this result:
level 1, cost for next 8
level 2, cost for next 16
level 3, cost for next 24
level 4, cost for next 32
level 5, cost for next 40
level 6, cost for next 48
level 7, cost for next 56
level 8, cost for next 64
level 9, cost for next 72
level 10, cost for next 80
level 11, cost for next 88
level 12, cost for next 96
level 13, cost for next 104
level 14, cost for next 112
level 15, cost for next 120
level 16, cost for next 128
level 17, cost for next 136
level 18, cost for next 144
level 19, cost for next 152
level 20, cost for next 160

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants