I am writing a small program and I need to calculate integer partitions of a handful of numbers. In my code I just run IntegerPartitions[k,{n}] and then iterate through the results. Since I only use the result through this iteration I was wondering if there is an easy way to get integer partitions one at a time. This way I am hoping to reduce memory usage and if I am lucky also time.

EDIT: Well yes, I should have given an example. So with IntegerPartitions[6,{3}] we get the result {{4,1,1},{3,2,1},{2,2,2}}, I get this and feed it in a loop and treat each one separately. The problem is that I don't need all of them at once, so when the numbers are big, this list becomes unnecessarily big itself. It would be ideal to implement the functionality of the function IntegerPartitions inside my loop and have only one of the partitions each time.

  • $\begingroup$ I did, I hope it helps. $\endgroup$
    – tst
    Dec 18, 2012 at 23:02
  • $\begingroup$ Of course n and k are known from the beginning. My problem is that the result is generated all at once. The length of the list can be well over one million. (By the way, how do you produce the grey box with the command?) $\endgroup$
    – tst
    Dec 18, 2012 at 23:38
  • $\begingroup$ I am not asking whether I can make ItegerPartitions to give one partition at a time, but how can I write my version of it that does that. I tried but it became too messy too quickly for me to handle. $\endgroup$
    – tst
    Dec 18, 2012 at 23:41
  • $\begingroup$ OK, thanks, this is actually what I am doing now. $\endgroup$
    – tst
    Dec 18, 2012 at 23:52
  • $\begingroup$ You can use the 4'th argument like so: IntegerPartitinos[k,{n},All,{i}] to get the i'th one, I doubt you'll see any speed increase but hopefully memory-usage might decrease $\endgroup$
    – ssch
    Dec 19, 2012 at 0:07

1 Answer 1


To get the first 10 integer partitions of 15...

IntegerPartitions[15, All, All, {1, 10}]

{{15}, {14, 1}, {13, 2}, {13, 1, 1}, {12, 3}, {12, 2, 1}, {12, 1, 1, 1}, {11, 4}, {11, 3, 1}, {11, 2, 2}}

To get the 8th to 12th integer partitions of 15...(to show the overlap)...

IntegerPartitions[15, All, All, {8, 12}]

{{11, 4}, {11, 3, 1}, {11, 2, 2}, {11, 2, 1, 1}, {11, 1, 1, 1, 1}}

From the documentation...


As the above shows, IntegerPartitions also allows for a range as the fourth argument.

  • $\begingroup$ I am aware of this but it is a lot slower. $\endgroup$
    – tst
    Dec 19, 2012 at 9:49

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.