Are there techniques to know how many times loops like these will run? How do you evaluate how many times some loops will run, when you start to complicate the boundaries conditions? For example, take this function:

def f(n):
    s = 0
    for i in range(n):
        for j in range(i, (n + i**2) // (n - i) + 1):
            s += 1
    return s

for i in range(10):
    print(i, "-&gt;", f(i))


Output:

0 -&gt; 0
1 -&gt; 2
2 -&gt; 5
3 -&gt; 10
4 -&gt; 17
5 -&gt; 28
6 -&gt; 42
7 -&gt; 60
8 -&gt; 83
9 -&gt; 109
```
If you approximate this as an integral of the second for loop you get, substituting $k=(n-i)$:

$$I_n = \int_1^n \frac{n+(n-k)^2-(n-k)+1}{k} dk = \int_1^n 1+\frac{(n-k)^2+1}{k} dk$$

according to WA, the corresponding antiderivative is of the indefinite integral is:

$$(n^2+1)log(k) + \frac{1}{2}k(k-4n+2) +C$$

Thus, evaluating at $k=1$ and $n$ we get:

$$I_n = (n^2+1)log(n)+\frac{-3+6n-3n^2}{2}$$

So, the discrete sum should approximately reflex that. I graphed it out on desmos, and it seems to show that the exact error grows infinitely, they are asymptotically equal.