Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Generator of integer partitionts of n

by sfink (Deacon)
on Aug 29, 2004 at 04:23 UTC ( #386693=note: print w/ replies, xml ) Need Help??


in reply to Generator of integer partitionts of n

And my take (this just prints things out; it would be easy enough to accumulate them into an array or whatever):

#!/usr/bin/perl part(shift, []); # Print all partitions of $n using numbers no greater # than the last element of $sofar sub part { my ($n, $sofar) = @_; if ($n == 0) { print join(" ", @$sofar), "\n"; return; } my $max = @$sofar ? $sofar->[-1] : $n; my $to = ($max > $n) ? $n : $max; for (reverse 1 .. $to) { part($n - $_, [ @$sofar, $_ ]); } }


Comment on Re: Generator of integer partitionts of n
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-07-23 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (131 votes), past polls