And these counts can be computed much faster using:

sub Prod { my( $i, $j )= @_; my $f= $i; $f *= $j if $i < $j; for my $p ( $i+1..$j-1 ) { $f *= $p*$p; } return $f; } sub Count { my( $parens, $len )= @_; return 1 if 1 == $len; return Prod($parens+1,$parens+$len)/Prod(1,$len) }
Taking $len==4 for example, that boils down to:
(P+1)*(P+2)^2*(P+3)^2*(P+4)/1/2^2/3^2/4
which is an interesting equation. A mathematician might write it as:
(P+L)!/P!*(P+L)!/P!*L/(P+1)/(P+L)/L!/L! or [(P+L)!/P!/L!]^2*1*L/(P+1)/(P+L)
And it is interesting to me that I need to test for 1==$len but I don't need to test for 0==$parens.

Note that replacing $len with $parens+1 and $parens with $len-1 gives us the same values.

So I suspect the equation can be rewritten as (mostly) the product of two binomial coefficents (number of different subsets of size S you can pick from a set of size N). But I don't have the time to figure out all of the off-by-ones at the moment.

Update:

[(P+L)!/P!/L!]^2*1*L/(P+1)/(P+L) == [ C(P+L,P) ] * ?? where ?? == (P+L)!/P!/L!*1*L/(P+1)/(P+L) == (P+L)!/(P+L) /P!/(P+1) /L!*L == (P+L-1)! /(P+1)! /(L-1)!
But C(P+L,P+1) == C(P+L,L-1) == (P+L)!/(P+1)!/(L-1)! so ?? == C(P+L,P+1)/(P+L). So the number of ways to put P parens into a string of length L is
C(P+L,P)*C(P+L,P+1)/(P+L) or C(P+L,L)*C(P+L,L-1)/(P+L) or ...
...I think. (:

Now, someone just needs to explain why this formula makes sense. Then we'll have worked the problem "backward" and in future math classes the solution can be presented in the "forward" direction and we can intimidate another generation of students into thinking that they'd never be able to solve math problems... thus continuing a long tradition of mathematicians. ;)

                - tye

In reply to Re^2: Parens permutations (formula) by tye
in thread Parens permutations by artist

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, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.