-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition.pm
More file actions
293 lines (224 loc) · 6.72 KB
/
Partition.pm
File metadata and controls
293 lines (224 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Integer::Partition.pm
#
# Copyright (c) 2007-2013 David Landgren
# All rights reserved
package Integer::Partition;
use strict;
use warnings;
use vars qw/$VERSION/;
$VERSION = '0.05';
=head1 NAME
Integer::Partition - Generate all integer partitions of an integer
=head1 VERSION
This document describes version 0.05 of Integer::Partition, released
2013-06-23.
=head1 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
=head1 DESCRIPTION
C<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.
=head1 METHODS
=over 8
=item new
Creates a new C<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).
=cut
sub new {
my $class = shift;
my $n = shift;
if (!defined $n) {
require Carp;
Carp::croak("missing or undefined input");
}
elsif ($n =~ /\D/ or $n < 1) {
require Carp;
Carp::croak("$n is not a positive integer");
}
elsif ($n != int($n)) {
require Carp;
Carp::croak("$n is not an integer");
}
my $arg = shift;
my $forward = 0;
if (defined $arg and ref($arg) eq 'HASH' and exists $arg->{lexicographic}) {
$forward = $arg->{lexicographic};
}
my @x;
if ($forward) {
@x = (1) x ($n+1);
$x[0] = -1;
}
else {
@x = (1) x $n;
$x[0] = $n;
}
my $self = {
n => $n,
x => \@x,
h => $forward ? 1 : 0,
m => $forward ? $n - 1 : 0,
count => 0,
forward => $forward,
};
return bless $self, $class;
}
=item next
Returns the partition, or C<undef> when all partitions have been
generated.
=cut
sub next {
my $self = shift;
if ($self->{forward}) {
if (++$self->{count} == 1) {
return [@{$self->{x}}[1..$self->{n}]];
}
elsif ($self->{x}[1] == $self->{n}) {
return;
}
elsif ($self->{count} == 2) {
$self->{x}[1] = 2;
return [@{$self->{x}}[1..$self->{n}-1]];
}
else {
if ($self->{m} - $self->{h} > 1) {
$self->{x}[++$self->{h}] = 2;
--$self->{m};
}
else {
my $j = $self->{m} - 2;
while ($self->{x}[$j] == $self->{x}[$self->{m}-1]) {
$self->{x}[$j--] = 1;
}
$self->{h} = $j + 1;
$self->{x}[$self->{h}] = $self->{x}[$self->{m}-1] + 1;
my $r = $self->{x}[$self->{m}]
+ $self->{x}[$self->{m} - 1] * ($self->{m} - $self->{h} - 1);
$self->{x}[$self->{m}] = 1;
$self->{x}[$self->{m}-1] = 1 if $self->{m} - $self->{h} > 1;
$self->{m} = $self->{h} + $r - 1;
}
return [@{$self->{x}}[1..$self->{m}]];
}
}
return [$self->{n}] unless $self->{count}++;
return if $self->{x}[0] == 1;
if ($self->{x}[$self->{h}] == 2) {
++$self->{m};
$self->{x}[$self->{h}--] = 1;
}
else {
my $r = $self->{x}[$self->{h}] - 1;
$self->{x}[$self->{h}] = $r;
my $t = $self->{m} - $self->{h} + 1;
while ($t >= $r) {
$self->{x}[++$self->{h}] = $r;
$t -= $r;
}
$self->{m} = $self->{h} + ($t ? 1 : 0);
$t > 1 and $self->{x}[++$self->{h}] = $t;
}
return [@{$self->{x}}[0..$self->{m}]];
}
=item reset
Resets the object, which causes it to enumerate the arrangements from the
beginning.
$p->reset; # begin again
=cut
sub reset {
my $self = shift;
my $n = $self->{n};
my @x;
if ($self->{forward}) {
@x = (1) x ($n+1);
$x[0] = -1;
}
else {
@x = (1) x $n;
$x[0] = $n;
}
$self->{x} = \@x;
$self->{m} = $self->{forward} ? $n - 1 : 0,
$self->{h} = $self->{forward} ? 1 : 0,
$self->{count} = 0;
return $self;
}
=back
=head1 DIAGNOSTICS
=head2 missing or undefined input
The C<new()> method was called without an input parameter, which
should be a positive integer.
=head2 C<n> is not a positive integer
The C<new()> method was called with zero or a negative integer.
=head2 C<n> is not an integer
The C<new()> method was called with a number containing a decimal
component. Use C<int> or C<sprintf '%d'> on the input if necessary.
=head1 NOTES
This module implements the Zoghbi and Stojmenovic ZS1 and ZS2
algorithms for generating integer partitions. See
L<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).
=head1 SEE ALSO
=over 8
=item *
L<http://en.wikipedia.org/wiki/Integer_partition>
The Wikipedia entry on integer partitions
=item *
L<http://www.site.uottawa.ca/~ivan/F49-int-part.pdf>
The original 1998 paper written by Zoghbi and Stojmenovic.
=back
=head1 BUGS
None known.
Please report all bugs at
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Integer-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
Pull requests on Github may be issued at
L<https://github.com/dland/Integer-Partition>.
=head1 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.
=head1 AUTHOR
David Landgren, copyright (C) 2007-2013. 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?
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
'The Lusty Decadent Delights of Imperial Pompeii';
__END__