How to find the current level from the given points?

98 Views Asked by At

I'm working on a game where every user starts with Zero points. Based on their points, they are allocated levels. In order to reach Level 1, the user has to earn 1000 points. In order to reach Level 2, the user has to earn an additional 2000 points (Level 1 Points + 2000 points) and so on. The levels are as follows:

Level 0 - 0 Points
Level 1 - 1000 Points (0 + 1*1000)
Level 2 - 3000 Points (1000 + 2*1000)
Level 3 - 6000 Points (3000 + 3*1000)
Level 4 - 10000 Points

The general algorithm for the points required to reach the next level is,

Previous Level Points + (1000 * Level to Reach).

I need help in finding the Level of a user if I'm given the points. For example, if a user has 17,000 points, I should be able to calculate that his Level is 5.

Thanks in Advance.

2

There are 2 best solutions below

0
On BEST ANSWER

As @MattiP. noted correctly, the numbers you are dealing with (when you remove the last 3 zeros) are triangular numbers.

The sequence of those numbers, generated as $$x_n = \sum\limits_{i=1}^n i = \frac{n\cdot(n-1)}2$$The first few terms are $$0,1,3,6,10,15,21,28,36,45,55...$$

So how do you use this information? Suppose the number of points we have is $X$. We solve $$n^2+n-\frac X{500}=0$$Which was derived by a simple algebraic manipulation of the above formula. Finally, we take the floor of $n$, i.e., $\lfloor n\rfloor$, to get the level.

1
On

Hint: (I'll leave out the 1000's, so multiply the terms by 1000) What you're calculating is $$ x_n = 1 + 2+ 3+ 4 + \ldots + n $$ There is an algebraic expression for this: $$ x_n = \frac{n(n-1)}{2} $$ I suggest you calculate some values for this and see how you can relate this to your problem.