# Rearranging Pokemon Experience Formula to make Level the Subject

As the title suggests, I am trying to rearrange some of the formulas for calculating experience based on level to be the other way around (calculating level based on experience).

I am having trouble with rearranging this formula for n (n being level):

$$EXP = \frac{n^3(100-n)}{50}$$

What I have done so far is:

1. multiplied the 50 out

$${EXP}\times{50} = {n^3(100-n)}$$

1. expanded the brackets $${EXP} \times {50} = 100n^3 - n^4$$

But I don't know how to continue from here to make n the subject so that I can calculate the Level based on Experience. Do I divide the 100 out, if so, would that affect $n^4$?

Note: The links to the original formulas can be found here.

• You made one error: you needed to multiply by 50, not divide. Anyway, the equation can only technically be solved analytically, using the cumbersome quartic formula. In practice you would use a numerical solution. – Ian Aug 4 '15 at 1:55
• @Ian I realised my error and quickly edited my question. Thank you for the advice. – Hayden Aug 4 '15 at 1:57
• I plugged it into Maple even and it refuses to give a closed form solution, even though it's definitely possible. Just not easy. – Matt Samuel Aug 4 '15 at 2:09

As 6005 answered, you try to find $n$ such that the equation $$f(n)=n^4-100 n^3 +50 a=0$$ and, according to the link, this formula applies for $0\leq n \leq 50$ (and $a<125000$). As said, analytical solutions of quartic polynomials are not so funny and it could be easier to use a numerical method such as Newton. Starting from a guess $n_0$, the method will update it according to $$n_{k+1}=n_k-\frac{f(n_k)}{f'(n_k)}$$ that is to say $$n_{k+1}=\frac{n_k^3 (3 n_k-200)-50 a}{4 (n_k-75) n_k^2}$$ So, start at, say $n_0=25$ and iterate.

For illustration purposes, let us take $a=56789$; the iterates will then be $$n_1=38.3406$$ $$n_2=35.3914$$ $$n_3=35.2673$$ $$n_4=35.2671$$ which is the solution for six significant figures. All of that can easily be done using Excel.

You can improve the starting point using $n_0=a^{1/3}$ (this will be clear if you make a log-log plot of $n^4-100n^3$) and the first iterate of Newton method will be $$n_1=\frac{\left(3 \sqrt[3]{a}-250\right) \sqrt[3]{a}}{4 \left(\sqrt[3]{a}-75\right)}$$ which seems to be very good. For the illustrating case, this will give $n_1=35.3986$.

A still better approximation could be obtained using the simplest Pade approximant (do not worry : you will learn about them !). It will give $$n\approx \frac{650 a^{2/3}-3 a-30000 \sqrt[3]{a}}{-5 a^{2/3}+900 \sqrt[3]{a}-37500}$$ which, for the test case, will give $n\approx 35.2802$.

More accurate formulae could be made. Just post if you want better approximations.

Edit

Taking into account your last comments, for an exact solution use Newton $$n_{k+1}=\frac{n_k^3 (3 n_k-200)-50 a}{4 (n_k-75) n_k^2}$$ and choose $$n_0=50\, \big(\frac a {125000}\big)^{0.4306}$$ obtained minimizing $$\Phi(\alpha)=\int_0^{50}\Big(\frac{n^3(100-n) }{50}-125000 \big(\frac n {50}\big)^\alpha \Big)^2 \,dn$$ This exactly matches both end points and leads to an error of less than one unit over the whole range. Applied to $a=56789$, this gives $n_0=35.5985$ and the iterative process converges to the exact solution in a couple of iterations.

A more empirical (but much better) model could be $$n \approx 43.2633 \,x^{0.345438}+6.63856\, x^{1.33377}$$ where $x=\frac a {125000}$ (notice that the exponents are almost $\frac 13$ and $\frac 43$). The maximum error is smaller than $0.1$ over the whole range. For the test value, the result would be $35.2603$.

Edit

We can get the exact solution to the problem. Let us define $N=\frac n {50}$ and $A=\frac a {125000}$. This makes the equation to solve $N^3(N-2)+A=0$ which is a "quite simple" quartic. Now, let us define $$Z=\sqrt[3]{\sqrt{3} \sqrt{27 A^2-16 A^3}+9 A}$$ $$T=\sqrt{\frac{2\ 2^{2/3} A}{\sqrt[3]{3} Z}+\frac{\sqrt[3]{2} Z}{3^{2/3}}+1}$$ This leads to $$N=\frac{T+1}{2}-\frac{1}{2} \sqrt{\frac{2}{T}-\frac{2\ 2^{2/3} Y}{\sqrt[3]{3} Z}-\frac{\sqrt[3]{2} Z}{3^{2/3}}+2}$$

• I did a power law fit, by doing log-log least squares of $y=100x^3-x^4$ on $x=1,2,\dots,50$. I get $y=\exp(4.8797)x^{2.802}=131.6x^{2.802}$. That inverts to $x=0.1753y^{0.3569}$. Converting back to the old variables, this gives $n=0.7082a^{0.3569}$. This is a decent fit until around $n=40$. In particular, it gives the correct integer part of $n$ in your test case, which $a^{1/3}$ does not. (This is still a good answer, I'm just providing a small refinement.) – Ian Aug 4 '15 at 16:49
• Note that the situation for rather large $n$ is pretty bad in both of our cases. For $a=4.5 \cdot 10^6$, the answer should be around $43$, but your guess is about $165$ while mine is about $168$. – Ian Aug 4 '15 at 16:56
• @Ian. The formula is supposed fot $a \leq 1250000$ – Claude Leibovici Aug 4 '15 at 19:07
• Oh. Then the $n$ range should only go up to $25$... – Ian Aug 4 '15 at 19:20
• @Hayden. The equation is $f(n)=n^4-100 n^3 +50 a$; so $f'(n)=4n^3-300n^2$ and for the update I wrote it. It is very simple. – Claude Leibovici Aug 5 '15 at 4:56

You are trying to solve $$n^4 - 100n^3 + 50 \text{EXP} = 0$$ for $n$. Since the highest power of $n$ is $4$, this is known as a quartic equation. Fortunately, just as there is the quadratic formula for when the highest power is $2$, there is a quartic formula for when the highest power is $4$. (In fact, there is no formula to solve these things when the highest power is $5$ or more!)

Unfortunately, the quartic formula is EXTREMELY long, as you can see in this image. But it is simpler when some of the coefficients are zero, as in your case. The instructions for solving the quartic can be found on Wikipedia. First you will need to compute $p$ and $q$, then $\Delta_0$ and $\Delta_1$, then $S$ and $Q$, and finally you can plug them in to get the roots.

(If you're having trouble with it, let me know in a comment and I may be able to come back later to do the computation myself.)

• Thank you for the reply, I will let you know how it goes when I get home. – Hayden Aug 4 '15 at 2:43