Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get challengers in pyramid ranking system

Tags:

algorithm

php

so i got a ranking system that 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 every person can challenge every person to the left in the same row and to the right in the row above.

So for example 18 can challange 13-17
Basically you can challenge further up the ladder the lower you are.

Any Idea on how to solve this as a function? When thinking about the problem i just come up with some pretty complex calculations for the range by calculating the layer of the pyramid through counting down but i'm sure there has to be an easy solution.

Some more exmaples 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

Btw, even though it might look like homework, i can assure you i'm already out of school for some years now. This is actually going into an archery ladder system i'm trying to digitize for the local archery club :)

like image 926
bardiir Avatar asked Jul 05 '12 20:07

bardiir


1 Answers

For player x it is easy to see that the upper value in the range is always x - 1. The tricky part is to find the lower value.

First notice that the number of people you can challenge is equal to the number of people in the row above you. You may find it easier to understand why this is true by looking at the following diagram where each oval contains exactly one of the people player 13 can challenge (9, 10, 11, 12):

showing relationship to triangular numbers

There are four people that number 13 can challenge, and four people in the row above player 13.


So we need to find the number of people in the row above x. Notice that the total number of people above x is a triangular number T(n) for some n. And the value of n is the number of people in the row above x.

To find triangular numbers you may find this formula useful:

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

The problem is to find the largest n such that T(n) < x.

You could use a loop to run through all possible values of n, calculating T(n) until it exceeds x. This would work, it's simple, and it would almost certainly be fast enough for your purposes.

But you can also get there more directly by using the inverse of the above quadratic equation:

inverse of triangulare number formula

The only adjustment that is needed is to first subtract 1 from x because we want the triangular number strictly smaller than x. Without that adjustment it would give the current row for exact triangular numbers, instead of the row above.

Using this formula and translating it to PHP we can get the result directly for any x:

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

Results:

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

See it working online: ideone

like image 151
Mark Byers Avatar answered Sep 28 '22 16:09

Mark Byers