Yes, yes, this has been done before (hasn't everything?), but I think this still comes out to be the most compact method to actually calculate and display partitions of integers. The previous thread had people just trying to calculate them in memory. This one takes a single integer on the command line and goes to town.
sub p{my($n,@e,$o)=@_;print if!$_{$_=join$",sort@_,$/}++;p(++$o,--$n,@ +e)while$n-1}p(shift)
90 characters! woo hoo! I only beat tilly's solution by 1 character, once I appended print@$_,$/for P(shift) to it. A more compact printer added on to it would render that one the winner.
Update: Ya know, one of these days, my golf game'll be good enough that I'll post and someone won't immediately respond and say "I can shave off another character!" heh. Props to liverpole and blazar. I incorporated their suggestions, plus another one I thought of to drop down to 83.
sub p{my($n,@e,$o)=@_;$_{$_=join$",sort@_,$/}++||print;p(++$o,$n,@e)wh +ile--$n}p pop
Update 2: And 80
sub p{my(@e,$o)=@_;$_{$_=join$",sort@_,$/}++||print;p(++$o,@e)while--$ +e[0]}p pop
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: integer partition golf
by liverpole (Monsignor) on Oct 18, 2006 at 20:55 UTC | |
Re: integer partition golf
by liverpole (Monsignor) on Oct 19, 2006 at 00:57 UTC | |
by shmem (Chancellor) on Oct 19, 2006 at 13:47 UTC | |
by cephas (Pilgrim) on Oct 19, 2006 at 14:03 UTC | |
Re: integer partition golf
by blazar (Canon) on Oct 18, 2006 at 21:14 UTC |