Exponential Price Growth Help

231 Views Asked by At

I am in the process of developing an online game. Unfortunately, I've run into an issue. I cannot figure out how to make the price of a 'level' increase at a proper rate.

I am trying to make a formula where users can purchase as many levels as they want at a time. The price of levels should increase exponentially and the total cost of a purchase should be the same no matter the amount that is purchased at a time.

For example, a player wants to purchase 1,000 levels. They could either type in 1,000 and purchase all of the levels desired at once.. OR they could keep typing in 1 and clicking the purchase button.

The total price at the end should be the same. Could anyone give me an example that would work for this?

Thanks!

1

There are 1 best solutions below

2
On

I think you're saying you want the price of the first level to be $X$ for some $X$, you want the price of the $n^{th}$ level to be $k^n\cdot X$ for some $k$, and you want to know what the cost of buying $N$ levels between $m$ and $m+N$ would be. So, you're asking what this sum is?

$$ \sum_{i = m+1}^{m+N} k^i\cdot X $$

If so, then, you can use

$$ \sum_{i=0}^{N}k^i \cdot X = X \cdot \frac{1 - k^{N+1}}{1-k} $$ to help you, as

$$ \sum_{i = m+1}^{m+N} k^i\cdot X = \sum_{i=0}^{m+N}k^i\cdot X - \sum_{i=0}^{m}k^i\cdot X \\ =X\cdot \left( \frac{k^{m+1} - k^{N+m+1} }{1-k}\right) $$

Having said that, if you're programming this for a human facing interface, you're not going to have an appreciable computation time savings using a formula like this versus just adding up all the prices for the levels they want.