This question already has an answer here:

Let $x_1,\dots,x_m$ be non-negative integers such that $\sum_{i=1}^m x_i=n$, where $m,n$ are given. How can I enumerate all such lists of $m$ integers that add to $n$?

Note that IntegerPartitions[n,{m}] counts two such lists as one if they are a permutation of each other, but I would count them as distinct.

Thus the list I want must have $\binom{m+n-1}{m-1}$ elements.


marked as duplicate by Mr.Wizard Sep 24 '18 at 15:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Apply[Sequence]@*Permutations /@ IntegerPartitions[n, {k}] $\endgroup$ – AccidentalFourierTransform Sep 24 '18 at 15:37
  • $\begingroup$ Try FrobeniusSolve[ConstantArray[1, m], n]. (I am sure this is a dupe.) $\endgroup$ – J. M. is away Sep 24 '18 at 15:38
  • $\begingroup$ @AccidentalFourierTransform Thanks, that works. I didn't know that Permutations considered repeated elements as identical. Feel free to post an answer. $\endgroup$ – becko Sep 24 '18 at 15:42
  • $\begingroup$ @J.M.issomewhatokay. Thanks, also works. But AccidentalFourierTransform's answer seems faster. $\endgroup$ – becko Sep 24 '18 at 15:42
  • $\begingroup$ Did you want non-negative integers or positive integers? The two suggestions in the comments are different. $\endgroup$ – Carl Woll Sep 24 '18 at 15:47