Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: How to restrict partitions

by toolic (Bishop)
on Jan 18, 2014 at 03:29 UTC ( [id://1071053]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to restrict partitions
in thread How to restrict partitions

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

Replies are listed 'Best First'.
Re^4: How to restrict partitions
by AnomalousMonk (Archbishop) on Jan 18, 2014 at 17:10 UTC

    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 MoreUtils module seems to be really handy, but I tried some bigger numbers & I might have to rethink the overall approach. Thx for the help though everybody.
Re^4: How to restrict partitions
by crunch_this! (Acolyte) on Jan 18, 2014 at 05:36 UTC
    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.
      In Perl, "distinct" means "hash":
      sub distinct { my %distinct; @distinct{@_} = (); return keys %distinct; }
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1071053]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2025-07-18 01:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.