1
$\begingroup$

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$?

$\endgroup$
4
  • $\begingroup$ Can you please illustrate the desired combinations with a few small examples? $\endgroup$
    – Kiro
    May 3, 2019 at 9:30
  • $\begingroup$ @Kiro, please see my edit. $\endgroup$
    – MGK
    May 3, 2019 at 9:38
  • $\begingroup$ Related: equivalent-nested-loop-structure (combinations_with_replacement) $\endgroup$
    – expression
    Jul 14, 2021 at 13:01
  • $\begingroup$ GroupTheory`Tools`Multisets[Range[n], k] $\endgroup$
    – matrix42
    Jul 22, 2022 at 10:41

1 Answer 1

2
$\begingroup$
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)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.