How to prove formula related to $2$-adic valuation / $2$-adic absolute value and binary expansion

Solution 1:

Your proposed formula is always true. To show this, first to reduce the algebra involved, define

$$m = \lfloor \log_2 n \rfloor, \; \; j = \nu_2(n) \tag{1}\label{eq1A}$$

Since $m$ is the index of the largest non-zero binary coefficient of $n$, this means

$$n = \sum_{i = 0}^{m}c_i 2^i, \; 0 \le c_i \le 1 \; \forall \; 0 \le i \le m \tag{2}\label{eq2A}$$

With just the first floor function value which is being summed, using \eqref{eq2A} gives

$$\begin{equation}\begin{aligned} \left\lfloor\frac{2n - 1 + 2^{k+1}}{2^{k+2}}\right\rfloor & = \left\lfloor\frac{\sum_{i = 0}^{m}c_i 2^{i+1} + 2^{k+1} - 1}{2^{k+2}}\right\rfloor \\ & = \left\lfloor\frac{\sum_{i = k+1}^{m}c_i 2^{i+1} + \sum_{i = 0}^{k}c_i 2^{i+1} + 2^{k+1} - 1}{2^{k+2}}\right\rfloor \\ & = \left\lfloor\frac{\sum_{i = k+1}^{m}c_i 2^{i+1}}{2^{k+2}} + \frac{\sum_{i = 0}^{k}c_i 2^{i+1} + 2^{k+1} - 1}{2^{k+2}}\right\rfloor \\ & = \left\lfloor\sum_{i = k+1}^{m}c_i 2^{(i+1) - (k+2)} + \frac{\sum_{i = 0}^{k}c_i 2^{i+1} + 2^{k+1} - 1}{2^{k+2}}\right\rfloor \\ & = \sum_{i = k+1}^{m}c_i 2^{i-k-1} + \left\lfloor\frac{\sum_{i = 0}^{k}c_i 2^{i+1} + 2^{k+1} - 1}{2^{k+2}}\right\rfloor \\ & = \sum_{i = k+1}^{m}c_i 2^{i-k-1} + \left\lfloor\frac{(c_k + 1)\left(2^{k+1}\right) + (\sum_{i = 0}^{k - 1}c_i 2^{i+1} - 1)}{2^{k+2}}\right\rfloor \\ \end{aligned}\end{equation}\tag{3}\label{eq3A}$$

Note the numerator of the fraction in \eqref{eq3A} is greater than or equal to $2^{k+2}$ iff $c_k = 1$ and there's at least one $c_i = 1$ for some $0 \le i \le k - 1$, with the latter condition only being true if $k \gt j$. To make this simpler to handle, define a boolean type indicator function of

$$B(e) = \begin{cases} 0 & e \text{ is false} \\ 1 & e \text{ is true} \end{cases} \tag{4}\label{eq4A}$$

Using this function, \eqref{eq3A} can be simplified to

$$\left\lfloor\frac{2n - 1 + 2^{k+1}}{2^{k+2}}\right\rfloor = \sum_{i = k+1}^{m}c_i 2^{i-k-1} + c_{k}B(k \gt j) \tag{5}\label{eq5A}$$

The second floor function being summed is basically the same, but with the powers of $2$ being $1$ larger, so it becomes

$$\left\lfloor\frac{2n - 1 + 2^{k+2}}{2^{k+3}}\right\rfloor = \sum_{i = k+2}^{m}c_i 2^{i-k-2} + c_{k+1}B(k + 1 \gt j) \tag{6}\label{eq6A}$$

With the third floor function which is summed, all of the terms with powers of $2$ less than $k + 2$ become part of the fraction, so the result is

$$\left\lfloor\frac{n}{2^{k+2}}\right\rfloor = \sum_{i=k+2}^{m}c_i 2^{i-k-2} \tag{7}\label{eq7A}$$

Using \eqref{eq5A}, \eqref{eq6A} and \eqref{eq7A} gives

$$\begin{equation}\begin{aligned} & \left\lfloor\frac{2n - 1 + 2^{k+1}}{2^{k+2}}\right\rfloor - \left\lfloor\frac{2n - 1 + 2^{k+2}}{2^{k+3}}\right\rfloor - \left\lfloor\frac{n}{2^{k+2}}\right\rfloor \\ & = \sum_{i = k+1}^{m}c_i 2^{i-k-1} + c_{k}B(k \gt j) - \left(\sum_{i = k+2}^{m}c_i 2^{i-k-2} + c_{k+1}B(k + 1 \gt j)\right) - \sum_{i=k+2}^{m}c_i 2^{i-k-2} \\ & = \sum_{i = k+1}^{m}c_i 2^{i-k-1} + c_{k}B(k \gt j) - 2\sum_{i = k+2}^{m}c_i 2^{i-k-2} - c_{k+1}B(k + 1 \gt j) \\ & = \left(c_{k+1} + \sum_{i = k+2}^{m}c_i 2^{i-k-1}\right) - \sum_{i = k+2}^{m}c_i 2^{i-k-1} + c_{k}B(k \gt j) - c_{k+1}B(k + 1 \gt j) \\ & = c_{k+1} - c_{k+1}B(k + 1 \gt j) + c_{k}B(k \gt j) \\ & = (1 - B(k + 1 \gt j))c_{k+1} + c_{k}B(k \gt j) \end{aligned}\end{equation}\tag{8}\label{eq8A}$$

Letting the result be $r$, using \eqref{eq2A} and \eqref{eq8A} gives

$$\begin{equation}\begin{aligned} r & = n - \sum_{k=0}^{\lfloor \log_2{n} \rfloor}\left(\left\lfloor\frac{2n-1+2^{k+1}}{2^{k+2}}\right\rfloor - \left\lfloor\frac{2n-1+2^{k+2}}{2^{k+3}}\right\rfloor - \left\lfloor \frac{n}{2^{k+2}} \right\rfloor\right)2^k \\ & = \sum_{k = 0}^{m}c_k 2^k - \sum_{k = 0}^{m}\left((1 - B(k + 1 \gt j))c_{k+1} + c_{k}B(k \gt j)\right)2^k \\ & = \sum_{k = 0}^{m}\left(c_k - ((1 - B(k + 1 \gt j))c_{k+1} + c_{k}B(k \gt j))\right)2^k \\ & = \sum_{k = 0}^{m}\left((1 - B(k \gt j))c_{k} - (1 - B(k + 1 \gt j))c_{k+1}\right)2^k \end{aligned}\end{equation}\tag{9}\label{eq9A}$$

If $n$ is odd, then $c_0 = 1$ and $j = 0$. Thus, $B(k \gt j)$ is $0$ only for $k = 0$, and $1$ otherwise. In addition, $B(k + 1 \gt j)$ would always be $1$. This means the coefficient of $c_{k}$ is $1$ only for $k = 0$, while $c_{k+1}$'s coefficient is always $0$. This gives $r = c_{0}2^{0} = 1$.

With $n$ being even, then $j \gt 0$. Since $c_k = 0$ for $k \lt j$, and $1 - B(k \gt j) = 0$ for $k \gt j$, the only non-zero value of $(1 - B(k \gt j))c_{k}$ is $c_j$ for $k = j$. Similarly, the only non-zero value of $(1 - B(k + 1 \gt j))c_{k + 1}$ is $c_j$ for $k + 1 = j \implies k = j - 1$. As such, in \eqref{eq9A}, the only non-zero summation terms are for $k = j - 1$ and $k = j$, giving

$$\begin{equation}\begin{aligned} r & = (0 - c_j)2^{j-1} + (c_j - 0)2^{j} \\ & = (0 - 1)2^{j-1} + (1 - 0)2\left(2^{j-1}\right) \\ & = 2^{j-1} \\ & = 2^{\nu_2(n) - 1} \end{aligned}\end{equation}\tag{10}\label{eq10A}$$