The stupid question is the question not asked PerlMonks

### Comment on

 Need Help??

With the recent golf events lately, i've been diving back into some number theory. The one part that always most amazed me were partitions. Oddly enough, i scoured the web looking for a formula to produce a list of the partitions for a number, and came up emtpy handed. This post is in SOPW becuase this algorithm isn't correct yet, but when finished it should be a decent go.

Let's start with some code:

```sub P {
# This is so that we don't go off the deep end with recursion : )
return [1,1] if \$_[0]==1&&\$_[1]==1;
return [1] if \$_[0]==1;

# If there's only one argument, return a list of lists containing
# the argument and it's partitions.
if (@_==1) {
return [@_], P(\$_[0]-1,1)

# Otherwise return a list of lists containing the arguments and
# the partitions of the opposites.
} else {
return [@_],
map({ [@\$_, \$_[1]] } P(\$_[0])),
map({ [\$_[0], @\$_] } P(\$_[1]))
}
}
[download]```
So firstly, the problem i'm having with the algorithm is that it doesn't return the else lists correctly. In particular it doesn't return the original [@_] part of the list. This is made obvious by running it and finding that the code returns a list including (for 5):
```5
4 1
3 1 1
2 1 1 1
1 1 1 1 1
And many duplicates (which i handle seperately)
[download]```
Note how <bold>3 2</bold> did not show up. Although Part(2) gets called, it's not returning correctly, or so it seems. i've tried to loop trace on paper, stepping through each recruse, but i think i missed something, because it should work.

Any enlightenment would be most helpful with this, yet again, after days of work, i'm blinded...

Golf: Create a subroutine (or set of subroutines) that returns a list of partitions of a number n.

My entry (which is rather long) is above if included with this snippet for removing duplicates (which could probably also be optimized). Currently i weigh in at a whopping 235 characters (without whitespace) if the following is included:

```sub u{map{[split//,\$_]}sort{\$b cmp\$a}keys%{{map{my\$t=join'',@\$_;\$t=>1}
+@_}}}
[download]```
nuf evah,
jynx

In reply to (Golf as well): List of Partitions by jynx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.
• Log In?
 Username: Password:

What's my password?
Create A New User
Chatterbox?
 [LanX]: M-x untabify [choroba]: I don't think they're multiple people. I was told "he or she" sounds old-fashioned and "they" is the way to say it [pryrt]: Those others were definitely offensive or unequivocably rude, I agree. [choroba]: (well, it comes from the 14th century, so labelling it as "modern" doesn't seem appropriate) [Your Mother]: "They" is becoming accepted but it irritates me sometimes. I tend to just pick she or he randomly or try to use "one." LanX thinks it is appropriate here :) [Your Mother]: They would think so. [jdporter]: ok, I need a recipe for piping lines "through" an external program which is itself a filter [jdporter]: without using a tmp file [1nickt]: tobyink perl -MTypes::Standard= is_Int -Mstrict -wE 'say 1 if is_Int 1.0'

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2017-05-24 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (186 votes). Check out past polls.