[Math] Convert nested for loop to mathematical expression

computer sciencenotation

Does anyone know how I can convert this nested for loop into a mathematical expression?

int idx=0;
for (int i=0; i<N-1; i++){
    for (int j=i+1; j<N; j++){
    out[idx++] = fn(i,j);
}}

Best Answer

If you are referring to algorithm complexity, every loop you have translates in a sum. Your algorithm thus have the following complexity:

$\sum_{i=0}^{N-1} \sum_{j=i+1}^{N} $ [...]

where [...] is the complexity of your function fn(i,j).

If you are referring the mathematical expression of the out variable, you seem to be requiring a matrix notation. Something along the lines of:

Let $A$ be a strictly triangular matrix where $A_{i,j} = fn(i,j)$.

If you need to explicitly explain a strictly triangular matrix, you can add:

such that $A_{i,j} = 0$ for $i \le j$

As shown here.