Creating tuples modulo index

I am looking for an algorithm (or a C-like implementation, without itertools available) that generates all tuples [a_0 a_1 ... a_ (n-1)] such that 0 <= a_i <= i + 1. Pointers to the literature are also welcome .

+4
source share
2 answers

something like that?

void printTuples (int n, int[] a, int i=0) { if (i == n) { //print a return; } for (int j=0; j<=i+1; j++) { a[i] = j; printTuples (n, a, i+1); } } 
+3
source

He called the countdown. Search Wikipedia for this. You can do it both recursively and iteratively.

Amir, he wants between 0 and i + 1, not between 0 and i. And I think that passing arrays on the stack is slower than using them globally.

I think you need something like this:

 int a[YOUR_LENGTH]; void backtracking (int n, int counter) { if (counter == n) { // do whatever return; } for (int j = 0; j <= counter + 1; ++ j) { a[counter] = j; backtracking(n, counter + 1); } } 
0
source

All Articles