Context: Online idle game, there is an upgrade that has a cost function of:
$Cost(n) = an^2+bn+c$
Which I understand gives a summation of:
$$Sum.Cost(n) = \frac{n(n+1)(n+2)}3$$
Where I get stuck is figuring out "If you have G gold and are at n=X how can you determine Y." Typically I solve by doing...
$G = Sum.Cost(X+Y) - Sum.Cost(Y)$
$Sum.Cost(X+Y) - Sum.Cost(Y) - G = 0$
I'm a bit stuck in that I found this general equation for cubics on Vanderbilt college's site but it's$ (a) $ as ugly as sin and $(b)$ seems like it's throwing NaNs back depending on the values of G and X due to complex roots.
Is there an easier way to do this I am just missing? Any help is appreciated. Worst case I stick with linear cost curves :P This can be solved using loops but it becomes compute intensive which is why I'm trying to find a more elegant solution.
First of all, Welcome to the site !
You do not face a cubic at all since expanding, the $Y^3$ term disappears and then you have $$G=\frac{1}{3} X (X+1) (X+2)+X (X+2) Y+X Y^2$$ which is just a quadratic in $Y$. The solutions are simply $$Y_\pm=\frac{\pm \sqrt{36 G X+12 X^2-3X^4}-3 X(X-2)}{6 X}$$