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, $_ ]); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://386693]
help
Chatterbox?
[1nickt]: chacham I have wanted to, and dones so, and so stated in a reply, and been roundly downvoted 2-1. (I assume for attempting to censor.)
[1nickt]: pryrt yes, I see. There is no way to differentiate without forcing to a string and all sort of weird stuff. It is enough. User 42 can get creative if s/he wishes.
[chacham]: i sent him a private message expressing my displeasure.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-05-24 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?