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
I'm pretty proud of this one. I think my golf game is improving. As always, let's reformat for clarity.
sub p{ my($n,@e,$o) = @_; print if ! $_{$_ = join$",sort@_,$/}++; p(++$o, --$n, @e) while $n-1; } p(shift)
This one's so tight it's gonna be a real bitch to explain. it's a recursive function, and each sub-call hands in one less than the number it previously had, in addition to a counter counting up to where it previously was, plus any other arguments it had received. I'll walk this through in a bit.
my($n,@e,$o) = @_; #note this is the same as: my $n = $_[0]; my @e = @_; my $o;
print if ! $_{$_ = join$",sort@_,$/}++;
This is the bulk of the magic. Internally, it pulls in and sorts the arguments it was handed. So, for example, if we're partitioning 15, on one call this function would receive (11,2,1,1) (in some order). So here we sort it (1,1,2,11) along with $/ (input record terminator, defaults to "\n", invaluable for obfuscations). This has the unfortuate side effect of printing the newline first, then the output, but que sera sera.
It's sorted so that we can subsequently return with the same arguments in a different order but not reprint. It assigns that sorted business to $_, and uses the whole ball of wax as a hash key which it post-increments.
We then call print w/o arguments (defaults to $_, which we just assigned back in that hash key), only if the value of the key in the hash == 0. That is, only the first time that string is encountered. This prevents duplicates.
We then recurse. We hand off an arbitrary counter that constantly goes up, our original number, decremented, and anything else we were originally handed in. Repeat as long as the number exists.
Sample runthrough: (unless I inserted a logic error)
p(5) outputs 5 calls p(1,4) outputs 1,4 calls p(2,3) outputs 2,3 calls(1,1,3) outputs 1,1,3 calls p(3,2) calls(2,2,1) outputs 1,2,2 calls(1,1,2,1) outputs 1,1,1,2 calls p(4,1) calls p(3,2,1) calls p(2,2,1) calls p(1,1,2,1) calls p(2,1,1,1) calls p(1,1,1,1,1) outputs 1,1,1,1,1
|
|---|
| 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 |