1

I have $K$ variables.

Each variable can take any value form a set with $N$ elements.

We have $N^K$ possible solutions (permutations with repetition, when at each time slot we can choose among $N$ elements each time). However, some of these $N^K$ possible solutions will provide the same offered rate (we do not care about the ordering). So, the possible solutions reduce to:

$\frac{(K+N-1)!}{K!(N-1)!}$

How can I generate all these possible combinations when $N=7$, $K=20$?

MGK
  • 565
  • 3
  • 9

1 Answers1

2
With[{n = 2, k = 3},
  Join @@ Table[IntegerPartitions[s, {k}, Range[n]], {s, k, n k}]]

{{1, 1, 1}, {2, 1, 1}, {2, 2, 1}, {2, 2, 2}}

With[{n = 7, k = 20},
  Join @@ Table[IntegerPartitions[s, {k}, Range[n]], {s, k, n k}]]

{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, ..., {7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6}, {7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}}

(230230 solutions)

Roman
  • 47,322
  • 2
  • 55
  • 121