Concrete Mathematics, Problem 1.16
\[ g(1)=\alpha \]
\[ g(2n+j)=3g(n)+\gamma n+\beta_j \]
\[ \mbox{for}\ j=0,1\ \mbox{and}\ n\geq1 \]
Solution
First let's take note of all of the applicable ways to describe $n$
\[ n=2^m+l=(1b_{m-1}b_{m-2}...b_1b_0)_2 \]
We can also define $g(n)$ as a linear combination of unknown functions of $n$ and their corresponding coefficients
\[ g(n)=a(n)\alpha + b(n)\beta_0 + c(n)\beta_1 + d(n)\gamma \]
The next step is to manually calculate $g(n)$, for the first few values of $n$, in an attempt to find a pattern. After observing the output of $g(n)$, we can easily see that $a(n)$ is a power of $3$. This observation hints at analyzing the output of $g(n)$ in base $3$
\[
\begin{array}{c|c|c|c|c|c|c}
n & n_2 & a(n)_3 & b(n)_3 & c(n)_3 & d(n)_3 \\
\hline
1 & 1 & 1 & 0 & 0 & 0 \\
\hline
2 & 1\color{red}{0} & 10 & \color{red}{1} & 0 & 1 \\
3 & 1\color{green}{1} & 10 & 0 & \color{green}{1} & 1 \\
\hline
4 & 1\color{red}{00} & 100 & \color{red}{11} & 0 & 12 \\
5 & 1\color{red}{0}\color{green}{1} & 100 & \color{red}{10} & \color{green}{1} & 12 \\
6 & 1\color{green}{1}\color{red}{0} & 100 & \color{red}{1} & \color{green}{10} & 20 \\
7 & 1\color{green}{11} & 100 & 0 & \color{green}{11} & 20 \\
\hline
8 & 1\color{red}{000} & 1000 & \color{red}{111} & 0 & 201 \\
\end{array}
\]
We can quickly find $a(n)$ with the repertoire method and an educated guess of $3^m$. Note that this guess is inspired by observing the output of $g(n)$
\[ \mbox{Let}\ g(n)=g(2^m+l)=3^m \]
\[ \mbox{Then}\ g(1)=g(2^0+0)=3^0=1=\alpha \]
\[ g(2n+j)=g(2^{m+1}+2l+j)=3(3^m)+\gamma n+\beta_j\]
\[ 3^{m+1}= 3^{m+1}+\gamma n +\beta_j\]
Which implies that
\[ 0= \gamma n+\beta_j \]
\[ \alpha=1, \beta_0=0, \beta_1=0, \gamma=0 \]
\[ 3^m =1a(n) + 0b(n) + 0c(n) + 0d(n) \]
Thus
\[ a(n) = a(2^m + l) = 3^m \]
In order to find $b(n)$, we must notice that for every binary bit $b_x$ in $n$, where $x < m$ and $b_x=0$, there is a ternary digit $1$ in the output of $b(n)$ at position $x$. This relationship is expressed in the above table in red and with a bit of set builder notation below. Note that $0\in\mathbb{N}$, as it's quite natural to have nothing
\[ b(n)=\sum \{3^x : (\exists x)(x\in\mathbb{N}, x < m, b_x=0)\} \]
Interestingly enough, finding $c(n)$ is similar with the exception that $b_x=1$. This is also expressed in the above table in green and below as follows
\[ c(n)=\sum \{3^x : (\exists x)(x\in\mathbb{N}, x < m, b_x=1)\} \]
After defining $a(n)$, $b(n)$ and $c(n)$, the following equation becomes almost trivial
\[ a(n)\alpha + b(n)\beta_0 + c(n)\beta_1 = (\alpha\beta_{b_{m-1}}\beta_{b_{m-2}}...\beta_{b_1}\beta_{b_0})_3 \]
Now we can find $d(n)$ with the repertoire method and a standard guess of $n$
\[ \mbox{Let}\ g(n)=n \]
\[ \mbox{Then}\ g(1)=1=\alpha \]
\[ g(2n+j)=3n+\gamma n+\beta_j\]
\[ 2n+j= 3n+\gamma n +\beta_j\]
Which implies that
\[ -n+j= \gamma n+\beta_j \]
\[ \alpha=1, \beta_0=0, \beta_1=1, \gamma=-1 \]
\[ n =1a(n) + 0b(n) + 1c(n) - 1d(n) \]
Thus
\[ d(n) = a(n)+c(n)-n \]
So now we have the solutions
\[ a(n) = 3^m \]
\[ b(n)=\sum \{3^x : (\exists x)(x\in\mathbb{N}, x < m, b_x=0)\} \]
\[ c(n)=\sum \{3^x : (\exists x)(x\in\mathbb{N}, x < m, b_x=1)\} \]
\[ d(n) = a(n)+c(n)-n \]
Here's a brief example of how to calculate $a(n)$, $b(n)$, $c(n)$ and $d(n)$
\[ \mbox{Let}\ n=19=2^4+3=(10011)_2\]
\[ a(19)=3^4=81=(10000)_3 \]
\[ b(19)=3^2+3^3=36=(1100)_3 \]
\[ c(19)=3^0+3^1=4=(11)_3 \]
\[ d(19)=81+4-19=66=(2110)_3 \]
This is good but, we can do better. Lets simplify this solution by finding another equation via the repertoire method
\[ \mbox{Let}\ g(n)=1 \]
\[ \mbox{Then}\ g(1)=1=\alpha \]
\[ g(2n+j)=3(1)+\gamma n+\beta_j\]
\[ 1= 3+\gamma n +\beta_j\]
Which implies that
\[ -2= \gamma n+\beta_j \]
\[ \alpha=1, \beta_0=-2, \beta_1=-2, \gamma=0 \]
\[ 1 =1a(n) -2b(n) -2c(n) +0d(n) \]
Thus
\[ b(n) = \frac{1}{2}(a(n)-1)-c(n) \]
Now we can express $b(n)$ in terms of $a(n)$ and $c(n)$, thus eliminating the summation. So let's put everything together
\[ g(n)=a(n)\alpha + (\frac{1}{2}a(n)-\frac{1}{2}-c(n))\beta_0 + c(n)\beta_1 + (a(n)+c(n)-n)\gamma \]
\[ g(n)=a(n)\alpha + \frac{1}{2}a(n)\beta_0-\frac{1}{2}\beta_0-c(n)\beta_0 + c(n)\beta_1 + a(n)\gamma+c(n)\gamma-n\gamma \]
\[ g(n)=a(n)(\alpha + \frac{1}{2}\beta_0+\gamma)+ c(n)(\beta_1 -\beta_0+\gamma)-n\gamma -\frac{1}{2}\beta_0 \]
Therefore, the closed form solution is
\[ \mbox{Let}\ S= \{3^x : (\exists x)(x\in\mathbb{N}, x < m, b_x=1)\} \]
\[ g(n)=3^m(\alpha + \frac{1}{2}\beta_0+\gamma)+ (\beta_1 -\beta_0+\gamma) \sum S- n\gamma -\frac{1}{2}\beta_0 \]
I hope this helps you understand.