For a game project with a leveling system, I have a function which calculates the amount of XP (experience points) to reach the next level, starting from the minimum amount required for the specified current level.
This function $f(c) = Δp$ is defined as following, $c$ being the current level and $Δp$ being the XP required to reach the next level:
$$f(c) = Δp = 5*(c^2)+(50*c)+100$$
My goal is to find a function $g(l) = p$ in which $p$ is the total amount of required XP to reach the level $l$.
I've came up with this sum solution, which adds the results of $f(c)$ together until $l$ is reached:
$$\sum_{c=0}^l f(c) = f(0) + f(1) + f(2) + ... + f(l)$$
The problem with my solution is that the implementation of this in my program gets very slow with increasing $l$, as it has to call $f(c)$ exactly $l$ times. This is a problem for me, because it is possible in my environment that very large $l$ exist. I am pretty sure that I am already using the most optimal implementation for my sum solution.
So I am asking, is there any other way I can solve my problem, which may be faster for computing? Preferably a way which does not take more calculations the larger $l$ becomes?
Thanks in advance.
The sum of the first $m$ squares is $$ \frac{m(m+1)(2m+1)}{6}. $$ The sum of the first $m$ integers is $$ \frac{m(m+1) }{2}. $$ With these formulas you can find the sum of the first $m$ values of $f$ as a function of $m$ without ever evaluating $f$.
Make sure you get the start end conditions right, beginning at $0$ and ending where you wish.
Then I think you have a simple inequality to solve to find the value of $m$ for which the sum you care about first exceeds some known number.