You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Counting the number of possible Brainfuck programs of a given length.
2021-01-05
{%- include mathjax.html -%}
Counting BF Programs of a Given Length
Date: 2008-07-05
Update: 2021-01-05 (formatting for the web)
Problem
There is a language, which I'll affectionately call the BF programming language, that is written using only $8$ different keyboard characters.
Of the $8$ characters, $2$ of them are brackets and must come in pairs for the program to be valid.
That is, when a [ appears, there must be a matching ] somewhere to the right.
The brackets don't have to appear next to each other.
How many valid BF programs can be written using $n$ characters?
This problem was given to me by Stefan Zwanenburg, and we spent a good week hacking up programs to generate solutions.
The explanation presented here is the process I went through to get the final solution.
Some neat things happened along the way, though it is by no means the best or most efficient way of tackling the problem.
Say $V(b,c)$ is a function that returns the total valid combinations of $b$ open brackets and $c$ closed brackets.
You can take the solution step by step, starting with no brackets at all, and adding pair of brackets with each consecutive step.
The count of possible BF programs at each step can be added together to get the final answer.
For each step, there are:
$j$ open brackets and therefore $j$ closed brackets,
$\binom{n}{n-2j}=\binom{n}{2j}$ ways the non-bracket characters can be arranged,
$6^{n-2j}$ possible combinations of non-bracket characters in those spaces, and
$V(j,j)$ ways the brackets can be arranged.
Since the amount of brackets cannot exceed the specified amount of characters $n$, the pairs of brackets $j$ cannot exceed $\frac{n}{2}$.
Since there can only be an integer amount of pairs $(j\in\mathbb{Z})$, we can use the floor function $\left\lfloor \frac{n}{2}\right\rfloor$.
Which still leaves $V(j,j)$ to be dealt with.
When $j=0$, it must be true that $V(j,j)=1$ for the first term in the above summation to make sense.
Through trial, it can be found for $j=1,2,3$ that $V(j,j)$ yields $1,2,5$
Finding $V(4,4)=14$ begins to stress a stricter method.
It is beneficial to work through it a bit, I used my fingers; those of the right hand as open brackets and closed brackets from the right.
Meshing them together, the "bracket" combination is valid if and only if each finger of the right hand is somewhere above the matching finger on the left hand.
It helped to attack the problem vertically for some reason.
$V(5,5)=42$ is harder to count "by hand" since thumbs don't reach far enough to keep the layout straight.
So following that pattern, reversing the order of coefficients, and starting with $j=2$ on the first row, the coefficients in the expansions for $V(j,j)$ are as follows:
Summing the coefficients of the expansion for $V(j,j)$ gives the highest coefficient of in the expansion of $V(j+1,j+1)$.
Computationally, this isn't so bad since you only need to keep one row of coefficients and work your way up with the values of $j$.
But there is a simpler pattern to jump from $V(j,j)$ to $V(j+1,j+1)$ which can be (and really should have been) found just by looking at consecutive results.
For $j=0,1,2,\ldots,7$, terms of $\dfrac{V(j+1,j+1)}{V(j,j)}$ look like:
We must prove a lemma that the \ref{eq:prod-simp} simplification is valid.
Lemma.$\dfrac{2^{j}}{(2j)!}{\displaystyle \prod_{k=1}^{j}}(2k-1)=\dfrac{1}{j!}$, assuming that $j\in\mathbb{Z}^+$ and ${\displaystyle \prod_{k=1}^{0}}(2k-1)=1$.