I want to enumerate all vectors of length N, in which each element can have a value of [0 ... K] and for which the sum of all elements is SUM.
I solved this problem using a recursive function but I when I retyped in CUDA C I got a message that CUDA C doesn't support recursive functions. After this I made some changes and rewrote the function without using recursion, but the function was boolean and this also is not supported in CUDA C because main global function must be void without calling other functions. now I am out of ideas, any help?
The recursive function is the following:
private static void computeVectors(int[] n, int sum, int k, int k1, int i) {
if (sum == 0) {
printVectors(n, n.length);
} else if (i < n.length) {
for (int j = k; j >= 0; j--) {
if (j <= k1) {
n[i] = j;
computeVectors(n, sum - j, sum - j, k1, i + 1);
}
}
}
}
private static void printVectors(int p[], int n) {
for (int i = 0; i < n; i++) {
System.out.print(p[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
// TODO code application logic here
computeVectors(new int[4], 5, 3, 3, 0);
}
The output for this example is:
3200 3110 3101 3020 3011 3002 2300 2210 2201 2120 2111 2102 2030 2021 2012 2003 1310 1301 1220 1211 1202 1130 1121 1112 1103 1031 1022 1013 0320 0311 0302 0230 0221 0212 0203 0131 0122 0113 0032
0023