Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to partition an integer into $k$ integers all possible orderings. This can be done in the following way

integerPartitions[n_, {k_}] := Select[FrobeniusSolve[Table[1, {k}], n], FreeQ[#, 0] &]

For example,

integerPartitions[4, {2}]


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

But this is very slow if the partition number is large as compared to IntegerPartitions, which gives partitions in reverse lexicographic order. For instance, integerPartitions[60, {5}] will take more than 4 seconds in my laptop in contrast to 0 seconds of IntegerPartitions[60, {5}].

So my question is: what would be the most efficient Mathematica code for this problem?

share|improve this question
Can't you apply permutations to the results of IntegerPartitions ? – b.gatessucks Feb 12 '13 at 14:00
up vote 8 down vote accepted

This seems to be quite quick :

output = Flatten[Permutations /@ IntegerPartitions[60, {5}], 1]; // AbsoluteTiming
(* {0.245025, Null} *)

output // Dimensions
(* {455126, 5} *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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