Do you know where your variables are? PerlMonks

### Comment on

 Need Help??

I think there are a variety of improvements that could be made to the style of the code.

Firstly, initialising @a and @b can be done rather more simply:

```  @aa = @bb = (1) x \$nn;
.. which removes the need for the variable \$n

Secondly, the @ready array is being built up an element at a time, starting from element zero. So it would be simpler to remove the index calculations, and just use push:

```  push @ready, "@aa\n";
...
...

\$kk is used to count down from the start value to 0, and I'd find it clearer to express that directly at the head of the loop:

```  for (my \$kk = \$nn; \$kk > 0; --\$kk) {
...
}

It also seems odd that you declare @ready with my(), but none of the other variables. While writing everything with use strict is a good habit to get into, since there are no function calls in this code nor variables declared inside blocks there isn't much to be gained here. However consistency is always good, and you should either declare all your variables that way or none of them.

I don't understand the algorithm you are using at all: when I run the code for small values of \$nn I get many warnings, and the results include many duplications, odd spacing, and some zeros. Also, this algorithm always produces exactly 3n results, which is too many for small values of n and too few for larger values. (The first example I noticed of a missing partition was for n = 6 = 2 + 2 + 2.)

If I were attempting to produce an algorithm for this, I'd be inclined to start off with the assumption that I'd want an iterator, so I'd consider first how to define a canonical ordering for a partition (eg with the numbers sorted in descending order), and then consider how given one partition I could generate the next one.

This is fairly easy to do if you define the iterator function to take an additional parameter, but I don't want to give too much away here in case you still want the fun of working it out for yourself.

Hugo

In reply to Re: Generator of integer partitionts of n by hv
in thread Generator of integer partitionts of n by chiburashka

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.
• 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?

Create A New User
Chatterbox?
 [james28909]: and that is also anothe rpoint i made, i think it has to do with perception of the world around you. most people think of evolution on a human scale. why could life evolve on this planet? because this planet evolved in this solar system. and so on. [holli]: here's something for you to watch, James. I think you will like it [erix]: for the record: I have not downvoted anyone on that subthread that was my fault [james28909]: there are all kinds of things that had to happen to let life come to be. but at the same time, life may not be the end goal IF there is any kind of end goal lol [james28909]: well who is the person who gets to decide which behaviour is worthy of a downvote? a person with their own beliefs? xD [erix]: teleology -- I've never understood why that was thunk up [erix]: ( and when teleology was brought up, during my biology-study., I couldn't get an answer either ) [1nickt]: FTR I agree with you about "design." Just not sure about the trajectory of evolution. THere are a lot of dead-ends in the evolutionary paths. I suspect homo sapiens is just one of them. [james28909]: thanks for the vid holli, i do like it. thanks for sharing :)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-12-15 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (433 votes). Check out past polls.

Notices?