in reply to How to restrict partitions

use warnings; use strict; use Integer::Partition qw(); my $i = Integer::Partition->new(6); while (my $p = $i->next) { print join( ' ', @$p ), $/ if (@$p == 2) and ($p->[0] != $p->[1]); } __END__ 5 1 4 2

Replies are listed 'Best First'.
Re^2: How to restrict partitions
by crunch_this! (Acolyte) on Jan 18, 2014 at 03:16 UTC
    if (@$p == 2) and ($p->[0] != $p->[1]);

    Thx for the quick reply, that definitely did the trick but is there a more general way to do this? I'm just thinking of all the 'ands' I'd have to include. What if I want exactly, say, 5 summands?

      UPDATE: This does not satisfy the clarified requirements in the response.

      use warnings; use strict; use Integer::Partition qw(); my $i = Integer::Partition->new(6); while (my $p = $i->next()) { print join( ' ', @$p ), $/ if (@$p == 5) and not all_same(@$p); } sub all_same { my $same = 1; for (@_[1 .. $#_]) { if ($_ != $_[0]) { $same = 0; last; } } return $same; } __END__ 2 1 1 1 1

        Or, if all the values of the partitions must be unique, maybe something like (untested):

        use Integer::Partition qw(); use List::MoreUtils qw(uniq); my $i = Integer::Partition->new(6); my $n = 5; while (my $p = $i->next()) { do_whatever_with($p) if (@$p == $n) and (@$p == uniq @$p); }
        That works to give me the exact number of summands I'm looking for but it give me repeat summands. I get the idea behind the subroutine but I'm not sure how to change it so that it gives only partitions with distinct summands. I tried tinkering with the truth values & the != etc but it didn't seem to do the trick.