1
$\begingroup$
  • Let $a(n)$ be A347205. It is enough for us to know that

$$ a(2^m(2k+1)) = \sum\limits_{j=0}^{m}a(2^jk), \\ a(0) = 1 $$

  • Let $b(n)$ be A329369. It is enough for us to know that

$$ b(2^m(2k+1)) = \sum\limits_{j=0}^{m}\binom{m+1}{j} b(2^jk), \\ b(0) = 1 $$

  • Let $T(n,k)$ be A101211 (i.e, triangle read by rows: $n$-th row is length of run of leftmost $1$'s, followed by length of run of $0$'s, followed by length of run of $1$'s, etc., in the binary representation of $n$).

  • Let $c(n)$ be A005811 (i.e., number of runs in binary expansion of $n$ ($n>0$); number of $1$'s in Gray code for $n$).

  • Suppose that we want to compute $b(n)$ without recursion. Is there a way to do it? Presumably, there should be at least one way. So here it is:

  • Start with the vector $s$ of ones with the length $L=\sum\limits_{i=1}^{\frac{c(2n)}{2}}T(2n,2i)$. Also, let $t$ be an identical vector. After that for $i$ from $1$ to $\frac{c(2n)}{2}$ apply $A=\sum\limits_{q=1}^{i-1}T(2n,2q)$ and then (staying into the first cycle) for $j$ from $1$ to $T(2n,2i-1)$ and for $k$ from $A+2$ to $L$ apply $$ t_k = \sum\limits_{q=1}^{k-A}\binom{k-A}{q-1}s_{q+A}. $$

  • After ending the last cycle and while we stay at the second cycle, we also need to apply $s = t$.

I conjecture that $s_L$ after the whole transformation is equals to $b(n)$.

Here is the PARI/GP program to check it numerically:

a(n) = if(n == 0, 1, my(A = valuation(n, 2), B = n\2^(A+1)); sum(j=0, A, a(2^j*B)))
b(n) = if(n == 0, 1, my(A = valuation(n, 2), B = n\2^(A+1)); sum(j=0, A, binomial(A+1, j)*b(2^j*B)))
a1(n) = {my(n = 2*n, A = 1, v1, v2, v3); v1 = [];
for(i=0, logint(n, 2), 
if(bittest(n, i) == bittest(n, i+1), A++,
v1 = concat(v1, A); A = 1));
v1 = Vecrev(v1);
for(i=2, #v1/2, v1[2*i] += v1[2*(i-1)]);
v2 = vector(v1[#v1], i, 1); v3 = v2;
for(i=1, #v1/2, 
A = if(i==1, 0, v1[2*(i-1)]);
for(j=1, v1[2*i-1],
for(k=A+1, #v2,
v3[k] = sum(q=1, k-A, v2[q+A]));
v2 = v3));
v2[#v2]}
b1(n) = {my(n = 2*n, A = 1, v1, v2, v3); v1 = [];
for(i=0, logint(n, 2), 
if(bittest(n, i) == bittest(n, i+1), A++,
v1 = concat(v1, A); A = 1));
v1 = Vecrev(v1);
for(i=2, #v1/2, v1[2*i] += v1[2*(i-1)]);
v2 = vector(v1[#v1], i, 1); v3 = v2;
v4 = vector(#v2+1, i,
vector(i, j, j == 1 || j == i));
for(i=3, #v4, for(j=2, i-1, 
v4[i][j] = v4[i-1][j] + v4[i-1][j-1]));
for(i=1, #v1/2, 
A = if(i == 1, 0, v1[2*(i-1)]);
for(j=1, v1[2*i-1],
for(k=A+1, #v2,
v3[k] = sum(q=1, k-A, v4[k - A + 1][q]*v2[q+A]));
v2 = v3));
v2[#v2]}
test1(n) = a1(n) == a(n)
test2(n) = b1(n) == b(n)

Is there a way to prove it?

$\endgroup$
3
  • 1
    $\begingroup$ I presume in the definition of $L$ that the bound variable should be $k$ rather than $i$, but I'm not sure about the indexing of $T$. Is $L$ the number of $1$s in the binary expansion of $2n$, or the number of $0$s? $\endgroup$ Commented May 6 at 11:02
  • $\begingroup$ @PeterTaylor, thank you for comment! Done. Here $L$ is the number of $0$s in the binary expansion of $2n$. $\endgroup$ Commented May 7 at 6:33
  • 1
    $\begingroup$ My Sage code. I've generalised the binomial coefficients to an arbitrary triangle of coefficients $C_{n,k}$ and it still seems to work. Note that for your final question, $k$ is just $C_{1,0}$. I observe that the process can be rewritten by taking $t$ to be an infinite sequence of $1$s and then for each bit in $n$, most significant first, if it's a $1$ we multiply by the triangular matrix $C$ and if it's a $0$ we shift off the first element of $t$. The result is $t_0$ after the process completes. $\endgroup$ Commented May 7 at 10:35

1 Answer 1

4
$\begingroup$

Generalise to $$b(2^m(2k+1)) = \sum\limits_{j=0}^{m}C_{m+1,j} \, b(2^jk), \\ b(0) = 1$$

Consider the infinite matrices: $$M_0 = \begin{pmatrix} 0 & 1 & 0 & 0 & \cdots \\ 0 & 0 & 1 & 0 & \cdots \\ 0 & 0 & 0 & 1 & \ddots \\ \vdots & \vdots & \vdots & \ddots & \ddots \end{pmatrix} \hspace{2em} M_1 = \begin{pmatrix} C_{1,0} & 0 & 0 & 0 & \cdots \\ C_{2,0} & C_{2,1} & 0 & 0 & \cdots \\ C_{3,0} & C_{3,1} & C_{3,2} & 0 & \ddots \\ \vdots & \vdots & \vdots & \ddots & \ddots \end{pmatrix} \hspace{2em} t = \begin{pmatrix} 1 \\ 1 \\ 1 \\ \vdots \end{pmatrix}$$

I claim that if $n = 2^s + c_{s-1}2^{s-1} + c_{s-2}2^{s-2} + \cdots c_0$ then $$M_{c_0} M_{c_1} \cdots M_{c_{s-1}} M_1 t = \begin{pmatrix} b(n) \\ b(2n) \\ b(4n) \\ \vdots \end{pmatrix}$$

By induction on the bit length of $n$. We can take $n=0$ as a trivial base case even though it's not strictly covered by the claim, and the induction step from $n$ to $2n$ is trivial, so that leaves only the inductive step from $n$ to $2n+1$: i.e. that $$M_1 \begin{pmatrix} b(n) \\ b(2n) \\ b(4n) \\ \vdots \end{pmatrix} = \begin{pmatrix} b(2n+1) \\ b(2(2n+1)) \\ b(4(2n+1)) \\ \vdots \end{pmatrix}$$

Indexing from $0$ and substituting $n=k$, row $m$ of the LHS is $\sum_{j=0}^m C_{m+1,j} \, b(2^m k)$ and row $m$ of the RHS is $b(2^m (2k+1))$ so we reproduce exactly the recurrence.

I further claim that this matrix process is equivalent to your triple-looped algorithm, but I will not address that claim in detail.

In answer to the additional question about $d(2n+1)=kd(n)$, note that $d(2n+1) = C_{1,0} \, d(n)$ and nothing in the process or proof above requires $C_{1,0}$ to be $1$.

$\endgroup$
9
  • 1
    $\begingroup$ I basically got there by implementing your algorithm in Sage and then refactoring it to make it more idiomatic Python. The end result is what I link, and the process can be more-or-less read off from it. $\endgroup$ Commented May 7 at 13:33
  • 1
    $\begingroup$ To be frank, if you want to add code then I think you should use a single loop exploiting the formulae already present in OEIS: a(2n+1) = a(n) for n >= 0 and a(2^m*(2n+1)) = a(2^m*n) + a(2^(m-1)*(2n+1)) + a(2^(m-1)*(4n+1)) for m > 0, n >= 0. That will be both more efficient and more legible. $\endgroup$ Commented May 7 at 14:06
  • 1
    $\begingroup$ Python $\endgroup$ Commented May 7 at 14:41
  • 1
    $\begingroup$ It's dynamic programming rather than recursion. It computes the first $N$ values in $O(N)$ time and $O(N)$ memory. Your code takes $\Theta(n^3)$ time in the worst case to calculate a single value. $\endgroup$ Commented May 7 at 15:21
  • 1
    $\begingroup$ Just run $k$ from $A+1$ instead of $A+2$. I forgot I made that tweak when simplifying the code. As for efficiency: I do need to correct myself. For a single value, the matrix approach is $O(\lg^3 n)$ so it is better than dp. $\endgroup$ Commented May 7 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .