Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How to restrict partitions

by danaj (Friar)
on Oct 04, 2014 at 06:47 UTC ( [id://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.