How to get applicants in the pyramid ranking system

so I got a ranking system, which is basically a pyramid:

     01
    02 03
   04 05 06
  07 08 09 10
 11 12 13 14 15
16 17 18 19 20 21

Now each person can challenge each person to the left in the same line and to the right in the line above.

So, for example, 18 may claim 13-17
. Basically, you can challenge further up the stairs than lower.

Any idea on how to solve this problem as a function? When you think about a problem, I just come up with rather complicated calculations for the range, calculating the pyramid layer by counting, but I'm sure there should be a simple solution.

A few more examples for the ranges:
02 - 01
03 - 02
04 - 02-03
05 - 03-04
06 - 04-05
07 - 04-06
08 - 05-07
11 - 07-10
17 - 12-16

By the way, although it may look like homework, I can assure you that I have not studied for several years. This is actually part of the staircase archery system that I am trying to digitize for a local archery club :)

+5
source share
1 answer

It is xeasy for the player to see that the upper value in the range is always x - 1. The hard part is to find less value.

, , , . , , , 13 , (9, 10, 11, 12):

showing relationship to triangular numbers

, 13 , 13.


, x. , x T(n) n. n - x.

, :

T(n) = n * (n+1) / 2

, n , T(n) < x.

n, T(n), x. , , .

, :

inverse of triangulare number formula

, , - 1 x, , x. .

PHP, x:

$n = floor((sqrt(1 + 8 * ($x - 1)) - 1) / 2);
$lower = $x - $n;
$upper = $x - 1;

:

2: 1 - 1
3: 2 - 2
4: 2 - 3
5: 3 - 4
6: 4 - 5
7: 4 - 6
8: 5 - 7
9: 6 - 8
10: 7 - 9
11: 7 - 10
12: 8 - 11
13: 9 - 12
14: 10 - 13
15: 11 - 14
16: 11 - 15
17: 12 - 16
18: 13 - 17
19: 14 - 18
20: 15 - 19
21: 16 - 20

, : ideone

+9

All Articles