Integer partition generator

Generates a table of all integer partitions of integers from 0 to N.
1.1K Downloads
Updated 11 Jun 2012

View License

Integer partitions are the different ways to express an integer say "4" as a sum of other positive integers, in this case we would have 4=4,3+1,2+2,2+1+1,1+1+1+1. This program calculates all the partitions of every integer up to N which it stores in a cell array.

There is the option of only using partitions with up to "maxnum" integers, e.g. if maxnum = 3 the parition 1+1+1+1 (of 4) would be disallowed.

Also contained is a file to generate the number of integer partitions of an integer N using up to k integers which is used in the main file. Both files work via the recursive property of integer partitions and use integer class variables.

Update: Version two also allows the extra option of using only a restricted range of integers for the partition as well as a restricted number.

Example Useage:

Say I wanted to generate all the partitions of the numbers 0-100, using at most 6 numbers. Of which there are 3574454 this is calculated by:
table = integerparttable(100);
>> sum(table(:,7))

This set would be given by:
parts = intpartgen(100,6);
(Which takes ~ 20 seconds to compute on my machine.)

If I later wanted to extend this set to all the partitions up to 110, I can pass this list back to the function so it doesn't have to recalculate them all

parts = intpartgen(110,6,parts);

Note that parts{k+1} is the partitions of k (since arrays start from 1 and the partition for 0 is included as part{1})

To calculate the number of ways to pay a sum of money at most 10 coins
>>> intlist = intpartgen2(value,10,[],[0,1,2,5,10,20,50,100]);
>>> ways_to_pay = intlist{value+1};
(for exactly 10 coins remove the value zero from allowed numbers)

Cite As

David Holdaway (2024). Integer partition generator (https://www.mathworks.com/matlabcentral/fileexchange/36437-integer-partition-generator), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Logical in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.11.0.0

previously uploaded the wrong file!

1.10.0.0

Corrected help comments

1.9.0.0

Edited comment blocks to be continuous and show up when command "help" is used

1.8.0.0

Edited comment list to be one continuous block and thus show up when "help" is used

1.4.0.0

Version two allows the extra option of using only a restricted range of integers for the partition as well as a restricted number.

1.2.0.0

Updated Description to give example usage

1.1.0.0

Updated help, added feature to allow a cut off in partition length.

1.0.0.0