And these counts can be computed much faster using:
Taking $len==4 for example, that boils down to: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) }
which is an interesting equation. A mathematician might write it as:(P+1)*(P+2)^2*(P+3)^2*(P+4)/1/2^2/3^2/4
And it is interesting to me that I need to test for 1==$len but I don't need to test for 0==$parens.(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)
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:
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[(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)!
...I think. (:C(P+L,P)*C(P+L,P+1)/(P+L) or C(P+L,L)*C(P+L,L-1)/(P+L) or ...
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. ;)
- tyeIn reply to Re^2: Parens permutations (formula)
by tye
in thread Parens permutations
by artist
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |