1
// Copyright (C) 2006 Torsten Finke <fi@igh-essen.com>
3
// This program is free software; you can redistribute it and/or modify it under
4
// the terms of the GNU General Public License as published by the Free Software
5
// Foundation; either version 3 of the License, or (at your option) any later
8
// This program is distributed in the hope that it will be useful, but WITHOUT
9
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
// You should have received a copy of the GNU General Public License along with
14
// this program; if not, see <http://www.gnu.org/licenses/>.
19
// Joerg Arndt: Algorithms for programmers
20
// (http://www.jjj.de), 2006.
23
unsigned long *c_; // partition: c[1]* 1 + c[2]* 2 + ... + c[n]* n == n
24
unsigned long *s_; // cumulative sums: s[j+1] = c[1]* 1 + c[2]* 2 + ... + c[j]* j
25
unsigned long n_; // partitions of n
28
int_partition(unsigned long n)
31
c_ = new unsigned long[n+1];
32
s_ = new unsigned long[n+1];
47
for (unsigned long i=2; i<=n_; i++) { c_[i] = 0; }
49
for (unsigned long i=2; i<=n_; i++) { s_[i] = n_; }
52
const unsigned long *data() const { return c_; } // one-based!
56
// This algorithm was given by Torsten Finke
57
if ( c_[n_]!=0 ) return false; // last == 1* n (c[n]==1)
59
// Find first coefficient c[i], i>=2 that can be increased:
61
while ( s_[i]<i ) ++i;
65
unsigned long z = s_[i];
66
// Now set c[1], c[2], ..., c[i-1] to the first partition
67
// of z into i-1 parts, i.e. set to z, 0, 0, ..., 0:
73
c_[1] = z; // z* 1 == z