Divide a number in unequal increasing parts

7.2k Views Asked by At

I am working on a reward points system for my game. So as we know the points needed to reach a level increases level by level. For example if it needs 200 points to reach from level 1 to 2, it will probably need 1000 points to reach level 9 to 10.

So I need a formula which can tell me the level points(points needed to reach a level) once I provide the total number of levels and total number of points. The points should be increasing uniformly from level to level. For ex. if I have 10 levels and 20000 points, I should be able to find the points needed to reach a level. Those points should be increasing uniformly level by level.

5

There are 5 best solutions below

3
On BEST ANSWER

I am not sure if I interpret this question correctly, but I will post my answer anyway. I have $2$ answers actually, so I'll start with the simpler one. First of all though, I'll just state an assumption I'm making. I assume by "uniformly increasing level by level" what you want is an arithmetic progression of points i.e. the difference is additive. In other words, if $p_n$ is the points needed to reach level $n$ (from the previous level) then we want $p_{n+1} = p_n + d$ for some fixed $d$. I'm also going to take the convention that you start at level $0$ and that $p_0 = 0$ i.e. you start with no points. This seems like the most common-sense convention. Finally, let's call $L$ your number of levels, numbered $0$ through $L-1$. Level $L$ doesn't really exist but can be interpreted as the win-state of the game.

Case 1: Cumulative Points

In this interpretation, the points are cumulative. That is, the points earned from level $n$ carry over to level $n+1$, and count towards the points needed to get to level $n+2$. In this case, since the points difference is additive according to $p_{n+1} = p_n + d$, we get quite a simple formula for the points needed to reach a level: it is just an arithmetic series. So the points needed to reach the level $k$ is just $kd$. In particular, the points needed to reach level $L$, the win state, is the total number of points $T$, so we get $T = Ld$. Rearranging we can solve for $d$ in terms of $L$ and $T$:

$$d = \dfrac{T}{L}$$

And the (cumulative) points needed to reach level $k$ is then just:

$$p_k = kd = \dfrac{kT}{L}$$

Case 2: "Local" Points

In this case, the number of points you need to get from level $n$ to level $n+1$ is based only on points earned since entering level $n$. Again, this quantity must be increasing uniformly, so we get that the number of (local) points needed to get from level $k-1$ to level $k$ is $kd$. Now, because this is local, we must sum to get the total points. Then the total number of points $T$ is:

$$d + 2d + ... + Ld = d\sum_{i=1}^Li = \dfrac{dL(L + 1)}{2} = T$$

That step just applied the well-known formula for the triangle numbers. Now, rearranging to solve for $d$ we get:

$$d = \dfrac{2T}{L(L + 1)}$$

Now, the question asks for the "points needed to reach a level", which in this case will be local points earned only since starting that level. In this case, the number of points $p_k$ needed to reach level $k$ would be:

$$p_k = kd = \dfrac{2kT}{L(L + 1)}$$

Notes

Your results might not give you natural numbers, i.e. they may be fractions. This may be fine, but if you must work with natural numbers, then there are a few things you can do. One is that you can round up/down all the points i.e. do integer division, and settle for a slightly higher/lower total number of points. Another thing is to add the remainder from the division to the points needed to reach the last level. Another idea is to spread out the remainder randomly among different levels.

1
On

LOGARITHMIC SOLUTION!!

I am going to assume that you can only give points which are positive integers. Let $L$ be the total number of levels. I am going to consider two scenarios:

1) THERE IS NO LIMIT TO THE NUMBER OF LEVELS

I am considering a game such as Tetris or Pac-Man where there is no theoretical limit to the number of levels (or points). Define the series $\big(p_n\big)_{n \in \mathbb{N}}$ where \begin{equation} p_n = \lfloor{ \textrm{log}\left(n^k\right) \rfloor} \textrm{ mod } M \end{equation} for some $M,k \in \mathbb{N}$ where $\lfloor{\cdot \rfloor}$ denotes the floor function . The number $n$ denotes the number of points. Then a criteria for changing level is:

\begin{equation} \textrm{ change level if } p_{n+1} - p_{n} = (M-1) \end{equation}

You can see that the function is increasing making the level change (steep-steps) harder:

enter image description here

The code is pretty easy (this one is done in R):

level <- function(n,k,M){

  y <- floor(log(n^k)) %% M

  return(y)

}

for a given set of points $n$ you only need to check

if ( (level(n,k,M) - level(n+1,k,M)) == (M-1) )

You can play with the parameters $k$ and $M$. Or even making them random!

2) THERE IS A LAST LEVEL

Let $L$ be the last level.

First idea Let $p_\textrm{max} = \textrm{exp}\big( L \big)$ (assuming $L$ is small enough to calculate the exponential). You change levels every time $\textrm{log}\left( p \right)$ overpasses an integer. That is, each time $\textrm{exp}(p) = 1,2,3,\dots, L$

Here is a plot considering only $6$ levels, notice that reaching the next level requires increasingly more points (vertical lines show points required for the level):

enter image description here

0
On

Let $N$ be the number of level increases (= 1 less than the total number of levels if the starting level is included in the count).

The problem can only be solved in integers if the 'total' number of points $T$ (i.e., the points required to reach the last level) is a multiple of $N(N+1)/2$ (= the sum of the first $N$ integers) and in that case the additional requirement for the $k$-th increase is

$$k.\frac{2T}{N(N+1)}$$

The total requirement for the $k$-th increase (to reach level $k+1$) is then

$$\frac{T.k(k+1)}{N(N+1)}$$

In your example there are 9 level increases (10 levels) so $N(N+1)/2$ is 45. The total number of points 20000 is not a multiple of that; the next multiple of 45 is 20025 = 445 x 45. Level 2 is reached at

$$20025.\frac{1.2}{9.10}=445\ \hbox{points,}$$

level 3 at

$$20025.\frac{2.3}{9.10}=1335\ \hbox{points,}$$

(890 points more than level 2) etc.

0
On

I am not entirely sure I understand your question completely, but I hope the following answers it:

We have:

  • Total number of levels: $L$
  • Total number of points: $Y$
  • Initial step size: $x$

$L$ levels means $L-1$ steps from one level to the next, with the following points per step: $$\begin{array}{r|cccccccccccc} Level & 1 & & 2 & & 3 & \cdots & L-1 & & L\\ Step & & 1\to2 & & 2\to3 & & \cdots & & L-1\to L\\ Points & & x & & 2x & & \cdots & & (L-1)x \\ \end{array}$$

So the total number of points is $$x+2x+\ldots+(L-1)x=x\left(1+2+\ldots+L-1\right)=x\frac{L(L-1)}{2}$$ Setting this equal to $Y$, we can solve for $x$: $$Y=x\frac{L(L-1)}{2} \implies x=\frac{2Y}{L(L-1)}$$.

0
On

Let $f (n)$ be the lower threshold for the level $n$. Then the player needs to gain $f (n + 1) - f (n)$ points to reach level $n + 1$ from the starting point of $n$th level. From what you said I think you want $f (n + 1) - f (n)$ to be proportional with $n$. Say $f(n + 1) - f(n) = Cn$ for some absolute constant $C$, and essentially, $f (1) = 0$. Telescoping sum gives $$f (n) = f(1) + \sum_{k = 1}^{n - 1} (f (k + 1) - f(k)) = \sum_{k = 1}^{n - 1} Ck = \frac {C} {2} (n^2 - n).$$ Choose your $C$ yourself.

Also, if you have $S$ points at any given time, then the level $n$ you are at is $$n = \left [\frac {1 + \sqrt {1 + \frac {8S} {C}}} {2} \right],$$ where $[\cdot]$ is floor function, and the number of points needed to reach the next level is $$\frac {C} {2} (n^2 + n) - S.$$

Also, if you want (and tell me in comments), I can write a C code which takes the current points of the player and tells which level he is at and how many points are needed to reach the next level (in comments).