# Count amount of points for XY options (in poll) where options dont have equal amount of points

I have a code where I want to avoid using a for/each loop to count the amount of points options in poll have together. Reason is, every option has different ammount of points and I want to get number of votes by counting options and points they have together and then dividing total points (votes) by those points...

I have a list of options where every option has a different amount of points. I think this is easier to understand:

• Option 1 = 7 points
• Option 2 = 6 points
• Option 3 = 5 points
• Option 4 = 4 points
• Option 5 = 3 points
• Option 6 = 2 points
• Option 7 = 1 point

Is there some easy math way of getting to number of points (in this case 28) without using:

``````\$options = array("lala", "lalala", "flflf", "qddwq", "adslalsd", "wqdqowdq", "acaca");
\$points = 0; \$i = 1;
foreach(\$options as \$option){
\$points = \$points + \$i;
\$i++;
}
echo \$points;
``````

Or

``````\$points = 0;
\$options_num = 7;

for (\$i=1; \$i <= \$options_num; \$i++){
\$points = \$points + \$i;
}
echo \$points;
``````

I just want to make as small load on the server as possible, mainly when users won't be limited with amount of options they can insert and I am sure there has to be some simple math I can use, but I'm just not getting it.

• Eight minus option number? I don't see where you're getting 28 from. – Robert Harvey Jun 5 '15 at 14:46
• @RobertHarvey 7 + 6 + 5 + 4 + 3 + 2 + 1 = 28 – MiChAeLoKGB Jun 5 '15 at 14:51
• There's 8 options; you need a loop. No getting around it. Don't try to optimize something before you understand whether or not it is actually causing a performance problem. – Robert Harvey Jun 5 '15 at 14:53
• But I already mentioned that I am not limiting my users in amount of options, so there can be 100 or even more options. And then imagine thousands of users looking at results with hundreds options. I think there has to be some way of calculationg it without loops. – MiChAeLoKGB Jun 5 '15 at 14:57
• `(n * (n + 1)) / 2` ... See Triangular Numbers. – Robert Harvey Jun 5 '15 at 15:06

The sum

``````7 + 6 + 5 + 4 + 3 + 2 + 1 = 28
``````

Corresponds to a Triangular Number of the form

``````(n * (n + 1)) / 2
``````

i.e. `(7 * (7 + 1) / 2` = 28.

• Hi, I forgot to tell you that in your example you have `7 * 1` instead of `7 + 1`. I wanted to edit it myself, but edits have to be longer than 6 characters. – MiChAeLoKGB Jun 6 '15 at 15:53