in reply to Re: Parens permutations (order)
in thread Parens permutations

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

Replies are listed 'Best First'.
Re: Re^2: Parens permutations (formula)
by artist (Parson) on Aug 05, 2003 at 20:25 UTC
    ++tye for generating the excellent formula. Lots of interesting math involved in seemingly simple looking problem. Why the formula make sense is a very good question and I don't have answer at the moment.

    I had suspected the factorials once I saw symmetry and little similarity with pascal's triangle, but tye came out faster even with the solution.

    artist