Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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?
[stonecolddevin]: I will say John Mayer is an excellent guitarist, I don't like his music but he's pretty damn good
[erix]: scd: maybe you like John Mayall better :)
[erix]: (mayall = british blues)
[planetscape]: sleep well, Corion
[stonecolddevin]: erix seems pretty good, i listened to "So Many Roads" and it was nice. Not everyone's cup of tea but this guy can absolutely shred, he and the guy from Mastodon are definitely up there: https://www. youtube.com/watch? v=VzhMkSN9dQc

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2017-06-22 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (531 votes). Check out past polls.