NAME

Integer::Partition - Generate all integer partitions of an integer

VERSION

This document describes version 0.03 of Integer::Partition, released 2007-10-12.

SYNOPSIS

use Integer::Partition;

my $i = Integer::Partition->new(4);
while (my $p = $i->next) {
  print join( ' ', @$p ), $/;
}
# produces
4
3 1
2 2
2 1 1
1 1 1 1

my $j = Integer::Partition->new(5, {lexicographic => 1});
while (my $p = $j->next) {
  print join( ' ', @$p ), $/;
}
# produces
1 1 1 1 1
2 1 1 1
2 2 1
3 1 1
3 2
4 1
5

DESCRIPTION

Integer::Partition takes an integer number and produces an object that can be used to generate all possible integer partitions of the original number in either forward or reverse lexicographic order.

METHODS

new

Creates a new Integer::Partition object. Takes an integer as a parameter. By default, the partitions appear in reverse order, as the algorithm is slightly faster. Forward ordering uses a different, slightly slower algorithm (which is nonetheless much faster than any existing algorithm).

next

Returns the partition, or undef when all partitions have been generated.

reset

Resets the object, which causes it to enumerate the arrangements from the beginning.

$p->reset; # begin again

DIAGNOSTICS

missing or undefined input

The new() method was called without an input parameter, which should be a positive integer.

n is not a positive integer

The new() method was called with zero or a negative integer.

n is not an integer

The new() method was called with a number containing a decimal component. Use int or sprintf '%d' on the input if necessary.

NOTES

This module implements the Zoghbi and Stojmenovic ZS1 and ZS2 algorithms for generating integer partitions. See http://www.site.uottawa.ca/~ivan/F49-int-part.pdf for more information. These algorithms have been proven to have constant average delay, that is, the amount of effort it takes to produce the next result in the series.

They are the fastest known algorithms known for generating integer partitions (with the ZS1 reverse lexicographic order algorithm being slightly faster than the ZS2 lexicographic order algorithm).

SEE ALSO

BUGS

Please report all bugs at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Set-Partition|rt.cpan.org

Make sure you include the output from the following two commands:

perl -MInteger::Partition -le 'print $Integer::Partition::VERSION'
perl -V

ACKNOWLEDGEMENTS

Thanks to Antoine Zoghbi and Ivan Stojmenovic, for sharing their discovery with the world on the internet, and not hiding it in behind some sort of pay-wall.

AUTHOR

David Landgren, copyright (C) 2007. All rights reserved.

http://www.landgren.net/perl/

If you (find a) use this module, I'd love to hear about it. If you want to be informed of updates, send me a note. You know my first name, you know my domain. Can you guess my e-mail address?

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.