# Thread: Help with a math algorithm.

1. ## Help with a math algorithm.

I'm translating a small python script into C++, for a fellow internaut.
Though the script is less than 100 lines long, I haven't finished it. In fact I'm stuck a the very beginning.
This snippet is the culprit:
Code:
```if level < 10:
base_precision = 24 + (level - 1) * 4
elif level < 20:
base_precision = 24 + 8 * 4 + (level - 9) * 6
elif level < 30:
base_precision = 24 + 8 * 4 + 10 * 6 + (level - 19) * 8
elif level < 40:
base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + (level - 29) * 10
elif level < 50:
base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + 10 * 10 + (level - 39) * 12
elif level < 60:
base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + 10 * 10 + 10 * 12\
+ (level - 49) * 14
elif level < 70:
base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + 10 * 10 + 10 * 12 + 10 * 14\
+ (level - 59) * 16
elif level < 80:
base_precision = 24 + 8 * 4 + 10 * 6 + 10 * 8 + 10 * 10 + 10 * 12 + 10 * 14\
+ 10 * 16 + (level - 69) * 18
else:
base_precision = 24 + (8 * 4) + (10 * 6) + (10 * 8) + (10 * 10) + (10 * 12)\
+ (10 * 14) + (10 * 16) + (10 * 18) + 20```
The reason why he used those whimsical calculations elude me. The first thing I thought, was to remove the unnecessary steps.
Code:
```if (userTraits.level < 10)
basePrecision = (userTraits.level -1) * 4 + 24;
else if (userTraits.level < 20)
basePrecision = (userTraits.level -9) * 6 + 56;
else if (userTraits.level < 30)
basePrecision = (userTraits.level -19) * 8 + 116;
//........
else
basePrecision = 916;```
That was simple enough, and it worked. But I was struck with the idea that there is a pattern in those numbers.
After puling my hair for while I finally I came up with this formula:
Code:
`basePrecision = (level * 0.1 + 3.2) * level + 20`
It works alright for the most part, but it's a little off for certain values of level. e.g: if level = 34, basePrecision should be 246, but my formula returns 244.4
For such a small difference I thought playing around with ceil() or floor() would even it out. The problem is that it does, but it offsets other correct values.

My math skills are next to nonexistent. Perhaps you guys can see something there that I didn't, and come up with a more accurate formula.

Any help is appreciated.
Thanks.

2. I don't think you can find a reasonable approximation in a single formula. But you might come up with a loop.
The pattern is:
base_precision = 24
for each level below 10, add 4
etc.

Something like:
Code:
```int tlevel = level;
int base_precision = 24;
int group = 1;
while (tlevel >= 10) {
base_precision += 20 * group;
tlevel -= 10;
++group;
}
base_precision += 2 * group * tlevel;```
Except that this contains some off-by-one errors for the thresholds.

3. The bigger question is whether or not the original code is more correct that either of the simplifications listed in the thread.

But another approach is a to have a lookup table of base values plus a variable for the level you happen to be in
Code:
```int base[] = {24, 24 + 8 * 4, 24 + 8 * 4 + 10 * 6, 24 + 8 * 4 + 10 * 6 + 10 * 8, ...}

int val = base[level / 10] + (level % 10) * (4 *  2* level/10);```
Or something like that.

4. Replace 4 * 2 * level / 10 with 4 + 2 * level/10.