Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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 pondering the Monastery: (6)
As of 2015-07-29 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls