trying out possible combinations of varying formula with recursion
Can be done without recursion
You can do the same thing without recursion, by simply adding one to the rightmost dimension, and then &quot;carrying over&quot; when that number exceeds the one to its left.
Here is a sample implementation:
void loop()
{
    int i = 0;

    for (i = 0; i &lt; dim; i++) {
        number[i] = 1;
    }
    do {
        do_sth();
        for (i = dim - 1; i &gt; 0; i--) {
            if (number[i] &lt; number[i-1]) {
                number[i]++;
                break;
            }
            number[i] = 1;
        }
        if (i == 0) {
            if (number[0] &lt; max) {
                number[i]++;
            } else {
                return;
            }
        }
    } while (1);
}