December 9, 2023 - u/Cabslife

Please help me make this formula more concise

I am struggling at the moment...

I am designing a little XP collector in Notion based on completing habits/tasks, and I am trying to make each level scale up in its XP requirement (as opposed to using a floor function that would make the XP requirement for each level the same). Level 1 takes 3 XP, level 2 takes 6, level 3 takes 9, level 4 takes 12, so on and so forth. My goal is to include a formula that covers 100 levels.

The only way I can think to do this is by writing out an extremely long formula such as this:

if(prop(Total XP) < 3, 1, if(prop(Total XP) < 9, 2, if(prop(Total XP) < 18, 3, if(prop(Total XP) < 30, 4, if(prop(Total XP) < 45, 5, 0)))))

Where the above example (shown up to level 5 for brevity's sake) scales up the total XP requirement for each level, but manually (3 for level 1, 9 as 3+6 for level 2, 18 as 3+6+9 for level 3, etc.). In my actual formula, that'd mean I'd have to do that 100 times, which is fine (just extremely tedious), I'm just not familiar with Notion's ability to process long formulas like that. Is there a more concise way to accomplish this? I'm not great at math, and I'm especially not good at code, so if you have any tips please help me out. Thanks a bunch!


Quick answer

This is an arithmetic sequence. My first idea is to find the relationship between the two sequences of numbers 1,2,3,4,5… and 3,9,18,30,45….

The sum of the terms in a sequence is defined by :

$$ \sum_{i=1}^{lvl} i = {lvl}*\frac{(lvl+1)}{2} $$

Multiply by 3 and you get your XP :

$$ {XP} = {lvl}*\frac{(lvl+1)}{2}{*3} $$

prop("LVL").add(1).divide(2).multiply(prop("LVL")).multiply(3)

LVL to XP

We need to solve the equation to isolate LVL :

$$ lvl = \frac{-1 \pm \sqrt{1 + \frac{8xp}{3}}}{2} $$

Translating the equation for Notion and adapting it to demand (if less than) gives the following. This is the formula you asked for :

prop("Total XP").multiply(8).divide(3).add(1).sqrt().subtract(1).divide(2).ceil()

With this formula, if Total XP is less than or equal to the values you have requested, it gives you the LVL below. You start at 0 et got level 100 with 15150 XP.

XP = 0 Level 0
XP ≤ 3 Level 1
XP ≤ 9 Level 2
… …
XP ≤ 15150 Level 100

Note that this was done without any if.