# Creating an application with a level system and I would like to convert the values into an equation

Here is what I have so far: you start at level $0$ with $0$ XP. The objective is to gain XP to level up. Once you reach $100$ XP you get to level $1$, $300$ XP = Level $2$, $600$ XP = Level $3$, $1000$ XP = Level $4$, $1500$ XP = Level $5$, etc.

If you're at level $0$ it's a simple test to see if your XP is below $100$. If below then you stay at $0$, if $\geq$ then you're now at level $1$. Ok now the trickier part. Level $1$ and above:

Level = $1$, XP = $100 - 299$ , Equation: $$\frac{\text{user's XP}}{(\text{Level} + 2) \cdot 50}$$

If the user has an XP of $205$ the answer is $1.36666$. Less than $2$, so they stay at level $1$. This works all the way up to $299$, and when they hit $300$, they will level up to $2$.

Problem 1: anything below $150$, and the answer is less than $1$.
Problem 2: you can lose XP as well as gain it, so I have to find an equation that also takes into account XP that decreases.

Solutions I thought of while putting my kid to bed ($20$ minutes ago)

I'll have to find the upper value using the equation above, then I have to find the lower value using this equation

$$\frac{\text{user's XP}}{(\text{Level} + 1) \cdot 50}$$

If the result is greater than the upper value then level up. If the result is less than the lower value than level down If the result is in between the two than nothing.

Is this the best approach? Is there a better way?

-

Let's start with those numbers, $$0\quad100\quad300\quad600\quad1000\quad1500$$ Multiply by $8$, and add $100$: $$100\quad900\quad2500\quad4900\quad8100\quad12100$$ Take the square root, and divide by $10$: $$1\quad3\quad5\quad7\quad9\quad11$$ Now compare this to the levels you want: $$0\quad1\quad2\quad3\quad4\quad5$$ So we still have to subtract $1$ and divide by $2$. So, that's the algorithm: take the number of points, multiply by $8$, add $100$, take the square root, divide by $10$, subtract $1$, divide by $2$, round down to a whole number, that's the level.

-

$$\text{level} = \operatorname{int}\left(\sqrt{\frac{xp}{50}+\frac14}-\frac12\right)$$

where $\operatorname{int}\left(x\right)$ means you keep the integer part of $x$ and throw away the fraction.

For example, when xp is 1300, we calculate \begin{align} \operatorname{int}\left(\sqrt{\frac{1300}{50}+\frac14}-\frac12\right)& = \\ \operatorname{int}\left(\sqrt{26+\frac14}-\frac12\right)&=\\ \operatorname{int}\left(5.1234-\frac12\right)&=\\ \operatorname{int}\left(4.6234\right)&=\\ 4 \end{align}

But when $xp=1500$ exactly, the square root comes out to $\sqrt{30.25} = 5.5$ exactly, and then the rest of the calculation asks for $\operatorname{int}(5.5 - 0.5) = 5$.

I found this by taking the formula $$xp = 100\cdot\frac{\text{level}\cdot(\text{level}+1)}{2}$$

and solving it for the level in terms of the xp.

-