There's more than one way to do things PerlMonks

Re: How to restrict partitions

by danaj (Friar)
 on Oct 04, 2014 at 06:47 UTC ( #1102806=note: print w/replies, xml ) Need Help??

in reply to How to restrict partitions

Rather late, but I believe this works:
```use warnings;
use strict;
use ntheory qw/forpart/;
use List::MoreUtils qw/uniq/;

my \$sum = shift || 20;
my \$terms = shift || 4;

forpart { print "@_\n" if @_ == uniq @_ } \$sum,{n=>\$terms};

forpart is a partitions iterator similar to Pari/GP 2.6.1+, either unrestricted or with min/max/exact number of elements / size of elements. Pari/GP has some better optimizations, but even so, restricting the number of elements inside the XS code is a big win.

For cases where the sum and number of desired partitions gets large it helps to be a little smarter. Since we want unique elements, we know sequences like ... 1 1 1 1 1 won't work. The minimum sequence ends with ... 5 4 3 2 1, so we can find out the largest allowed element:

```use warnings;
use strict;
use ntheory qw/forpart/;
use List::MoreUtils qw/uniq/;

my \$sum = shift || 20;
my \$terms = shift || 4;

my \$amax = \$sum;
\$amax -= \$_ for 1 .. \$terms-1;

if (\$amax > 0) {
forpart {
print "@_\n" if @_ == uniq @_
} \$sum, {n=>\$terms, amax=>\$amax};
}
I think this is pretty straightforward to use and quite fast for most cases. As the sum goes up much over 100 this can still get out of control (Pari has the same issue). tye's Algorithm::Loops solution is better at weeding out non-uniques early in this case.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1102806]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2022-01-18 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2022, my preferred method to securely store passwords is:

Results (52 votes). Check out past polls.

Notices?