I have a for loop which gives a given integer sequence, for fixed parameters N and D :
int i = 0, j = 0;
for (int k=0; k<N; k++) {
sequence[k] = i;
if ((i += D) >= N) i = ++j;
}
I’d like to find a simple formula which reproduces this sequence, depending only on N and D (and the index k), like sequence[k] = D*(k%D)+ k/D (which doesn’t work). I tried hard but I just can’t find something which always work for any value of N and D!
Thanks!
Here’s the formula. At the moment it requires a conditional statement, but you can always express it via a function returning 0 or 1 if you want pure functional form.
I wrote it as Perl function, to ease testing (I tested for all N<=20 and D between 0 and N)