How do I calculate the amount of XP for a level where the first level is 110, and each level after is 10% more than the last. Preferably to do without a loop because the levels will have to be infinite and will need to be quickly calculated.
in js using a loop:
var xptest=110;
var lastLevel = 110;
for (var level = 2; xptest <= Number.MAX_SAFE_INTEGER || level < 100; level++) {
lastLevel*=1.1;
lastLevel = Math.round(lastLevel *1.1)
xptest+= lastLevel;
console.log('LEVEL',level,'('+lastLevel+' / '+xptest+')');
}
Answer
Let's work through some cases, given \$baseXP = 110\$ and \$increase = 1.1\$:
$$\begin{align} targetXP(1) &= baseXP\\ targetXP(2) &= baseXP + baseXP \cdot increase\\ targetXP(3) &= baseXP + baseXP \cdot increase + baseXP\cdot increase^2\\ ...\\ targetXP(n) &= baseXP + baseXP \cdot increase + ... + baseXP \cdot increase ^ {n-1}\\ \end{align}$$
If we multiply \$targetXP(n)\$ by \$increase\$, we find that all it does is shift the terms down one:
$$\begin{align}targetXP(n)&\cdot increase\\ &= baseXP \cdot increase + baseXP \cdot increase^2 + ...+ baseXP \cdot increase^n\end{align}$$
So if we subtract the original from this shifted version, all the terms except the first and last will cancel out, and we get...
$$\begin{align} targetXP(n) \cdot increase - targetXP(n) &= baseXP \cdot increase^n - baseXP\\ targetXP(n) \cdot (increase - 1) &= baseXP \cdot (increase^n - 1)\\ targetXP(n) &= baseXP \cdot \frac {1 - increase^n} {1 - increase}\end{align}$$
This is what's called a Geometric Series - you can read more about the math behind this here.
No comments:
Post a Comment