Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: (Golf as well): List of Partitions

by Masem (Monsignor)
on May 06, 2001 at 16:54 UTC ( #78346=note: print w/ replies, xml ) Need Help??


in reply to (Golf as well): List of Partitions

Here's one that is 65 characters, returns as array of arrays, but has duplicates (though the orders are unique).

sub P { $_[0]?map{my$c=$_;map[$c,@$_],P($_[0]-$c)}(1..$_[0]):[] }
Update: Same thing, but avoids repetitions: Total chars: 96
sub P { my$b=$_[1]||$_[0]; $_[0]?map{my$c=$_;map[$c,@$_],P($_[0]-$c,$c)}grep{$_<=$b}(1..$_[0]):[] }

Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain


Comment on Re: (Golf as well): List of Partitions
Select or Download Code
Re (tilly) 2 (non-recursive): (Golf as well): List of Partitions
by tilly (Archbishop) on May 06, 2001 at 20:39 UTC
    The first is a non-solution in my books, but the second is very good. But I was wondering if it was possible to do a reasonable non-recursive solution, and at 87 by my count, 94 counting the body, it seems like the best so far.
    sub P{ $n=pop;@a=[0];map{$i=$_;push@{$a[$i+$_]},map[$i,@$_],@{$a[$_]}for 0..$n}1..$n;@{$a[$n]} }
    I keep on thinking this should be improvable, but darned if I can see it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2014-08-22 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls