I'm trying to figure out of if there's a way to generate all unique sets of integers of length K, where each member has an upper bound of N, and a lower bound of M, without tracking which sets have already been generated, where the order doesn't matter.
To be clear: I'm not looking for ways to filter duplicates from a pre-computed set of sets.
I'm asking if there is an algorithmic approach to generate the unique combinations of values, without producing duplicate combinations/repetitions along the way that must be tested for and removed.
For example, I wrote this counter, which will generate all sets of length K with members bounded at N, but with duplicates.
JavaScript:
function count(k, n, m = 0) {
// Create counting columns
let cols = Array(k).fill(m);
let rows = [];
while (true) {
rows.push(cols.slice(0));
for (let j = 0; j < k; ++j) {
++cols[j];
if (cols[j] <= n) {
break;
} else if (j === k - 1) {
return rows;
}
// If no overflow, reset column and increment the next on next loop
cols[j] = m;
}
}
}
Output:
> let k=3, n=5, m=1
> count(k, n, m).join('\n');
"1,1,1
2,1,1
3,1,1
1,2,1
2,2,1
3,2,1
1,3,1
2,3,1
3,3,1
1,1,2
2,1,2
3,1,2
1,2,2
2,2,2
3,2,2
1,3,2
2,3,2
3,3,2
1,1,3
2,1,3
3,1,3
1,2,3
2,2,3
3,2,3
1,3,3
2,3,3
3,3,3"
But, as you can see, that produces 1,2,3 as well as 3,2,1.
An example case: if I'm searching for sums of cubes that equal a cube, I don't need to test 2^3 + 16^3 + 12^3 = 18^3
if I've already checked that 2^3 + 12^3 + 16^3 = 18^3
.
So I wouldn't want to generate an equivalent set again after having already tested an alternate order of the same terms.
Many thanks.