Code covered by the BSD License  

Highlights from
Partitions of an integer

4.88889

4.9 | 9 ratings Rate this file 27 Downloads (last 30 days) File Size: 6.73 KB File ID: #12009

Partitions of an integer

by John D'Errico

 

19 Aug 2006 (Updated 16 Jul 2008)

List all partitions of an integer

| Watch this File

File Information
Description

The money changing problem is a simple one to state. For example, how many different ways can one form change of a dollar (100 cents) by using only coins of denomination [1 5 10 25 50] ? (The answer is 292.)

Its an example of a general problem, i.e., in how many unique ways can an integer be partitioned as a sum of smaller positive integers?

http://en.wikipedia.org/wiki/Integer_partition

I wrote partitions to solve the fully general problem, but it can be used with restrictions too. You can constrain the set of elements in the sum, and the maximum number of times any one elements can appear, as well as fixing the total number of terms that will appear in the final sum.

See the demo for a few examples of use.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
partitiontable.m
This submission has inspired the following:
nsumk

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (11)
21 Aug 2006 urs (us) schwarz

a very useful snippet distinguished by excellent help (including sources of its algorithm), intuitive examples as well as a demo package, exhaustive error checking, good guidance for programmers, and a sleek computational engine.

07 Nov 2006 xiaole bai

Very good code! Very helpful!
Excellent !!!!

13 Nov 2006 Alexander Statnikov

Very helpful!

20 Jul 2008 Rakib Ahmed

Excellent codes with helpful documentation.

10 Aug 2008 Senol Korkmaz  
13 Feb 2009 Sergei Koulayev

Thank you John! Its not an easy problem to solve yourself.

21 Mar 2011 Arindra Guha

What algorithm is used or what is the theory behind how the partitions are generated by the program.

21 Mar 2011 John D'Errico

Suppose I wish to list out the integer partitions of 10, using the numbers [1,2,3,4]? The solutions are easy to delineate. Look at the largest element of the candidate members of the partition, in this case, pivot around the number 4. How many times can 4 potentially appear in that sum? Clearly, we may be able to form the total of 10 by including the number 4 a total of 0, 1, or 2 times. If so, then we can reduce the problem.

If 4 appears twice, then we need to find the list of all partitions of 10 - 2*4 = 2, made from the set [1,2,3].

If 4 appears once, then we need to find the list of all partitions of 10 - 1*4 = 6, made from the set [1,2,3].

If 4 appears not at all, then we need to find the list of all partitions of 10 - 0*4 = 10, made from the set [1,2,3].

I can rigorously argue that this procedure must generate all solutions, and do so rather efficiently, to the extent that is possible.

Now, call partitions to solve the problem. See that the last two solutions in the set have 4 appearing twice. There are exactly 2 ways to solve that problem. Before that, we see there were 7 ways to solve the partitions of 6. And there were 14 ways to solve the problem with 4 never appearing at all.

>> partitions(10,1:4)
ans =
    10 0 0 0
     8 1 0 0
     6 2 0 0
     4 3 0 0
     2 4 0 0
     0 5 0 0
     7 0 1 0
     5 1 1 0
     3 2 1 0
     1 3 1 0
     4 0 2 0
     2 1 2 0
     0 2 2 0
     1 0 3 0
     6 0 0 1
     4 1 0 1
     2 2 0 1
     0 3 0 1
     3 0 1 1
     1 1 1 1
     0 0 2 1
     2 0 0 2
     0 1 0 2

While partitions has other arguments that complicate the problem slightly by constraining the solution, the basic approach is exactly the same. Simply pivot around the largest member of the set of candidates to form that sum. Once you have determined the number of times 4 may appear in the sum, you can forget about it, and then worry about the rest.

Of course, had I tried to solve the problem partitions(500,1:500) by this approach, this would be a rather time (and memory) consuming task. My vpi toolbox has a tool to enumerate the number of integer solutions:

>> numberOfPartitions(500)
ans =
    2300165032574323995027

Or, if you prefer...

>> vpi2english(numberOfPartitions(500))
ans =
two sextillion, three hundred quintillion, one hundred sixty five quadrillion, thirty two trillion, five hundred seventy four billion, three hundred twenty three million, nine hundred ninety five thousand, twenty seven

But then, nothing I could do would solve that problem efficiently.

20 Sep 2011 Kammoun

good work

21 Sep 2011 Omar

Thanks a lot, I was looking for this function.

28 Sep 2011 Marcin  
Please login to add a comment or rating.
Updates
13 Nov 2006

Version 1.1: Allow the candidate set to be non-decreasing rather than strictly increasing

16 Jul 2008

Added a new option: a user defined number of terms in the sum.

Tag Activity for this File
Tag Applied By Date/Time
partitions John D'Errico 25 Mar 2012 06:45:36
integer John D'Errico 25 Mar 2012 06:45:36
partition John D'Errico 25 Mar 2012 06:45:36
sum John D'Errico 25 Mar 2012 06:45:36
subsets John D'Errico 25 Mar 2012 06:45:36

Contact us at files@mathworks.com