The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


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


This document describes version 0.05 of Integer::Partition, released 2013-06-23.


  use Integer::Partition;

  my $i = Integer::Partition->new(4);
  while (my $p = $i->next) {
    print join( ' ', @$p ), $/;
  # produces
  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


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.



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


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


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

  $p->reset; # begin again


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.


This module implements the Zoghbi and Stojmenovic ZS1 and ZS2 algorithms for generating integer partitions. See 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).



None known.

Please report all bugs at|

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

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

Pull requests on Github may be issued at


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.


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

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?


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