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

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
hi, what exactly you mean by if there is an easy way to get integer partitions one at a time Can you give a small example of what this means? – Nasser Dec 18 '12 at 22:55
I did, I hope it helps. – tst Dec 18 '12 at 23:02
What you asking for is impossible to do. The call IntegerPartitions[n, {k}] has to know the n and k values at that time it is called in order to generate the whole list. There is no way to tell it: please do not generate the whole list just right now and wait until I tell you when I really need something, and only then give me one number at a time I ask you to. I really think you are trying to solve a wrong problem here. list are immutable in Mathematica. How big a list we are talking about here? – Nasser Dec 18 '12 at 23:13
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
show 4 more comments

1 Answer

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

picture

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

 
discard

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.