Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 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.

share|improve this question
I did, I hope it helps. – tst Dec 18 '12 at 23:02
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?) – tst Dec 18 '12 at 23:38
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. – tst Dec 18 '12 at 23:41
OK, thanks, this is actually what I am doing now. – tst Dec 18 '12 at 23:52
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 – ssch Dec 19 '12 at 0:07

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.

share|improve this answer
I am aware of this but it is a lot slower. – tst Dec 19 '12 at 9:49

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.