1

I'm looking for a (simple) formula $t: \mathbb{N} \rightarrow \mathbb{N}$ to compute the number of trailing zeroes in the binary representation of a given $n \in \mathbb{N}$.

Like so:
$t(1) = t([1]_2) = 0$
$t(2) = t([10]_2) = 1$
$t(3) = t([11]_2) = 0$
$t(4) = t([100]_2) = 2$
etc.

I once found a rather complex formula back in 2016, that actually works, but is very clunky:
$t(n) = \sum_{k=1}^\infty cos( \frac{1}{2^k} \pi n) \prod_{j=1}^k cos( \frac{1}{2^j} \pi n)$
The basic idea behind this one is to sum cosines, which become $1$ at some $2^k$ and $0$ otherwise. It works, is provable by induction and I have worked with it, like e.g. here (sorry for IEEE paywall, it is just an example and not that much of importance).

However, I am still wondering, if there isn't any better or more simple way to describe $t(n)$, as the above formula is really hard to use. It also relies on non-integer functions (cosine), despite $t(n)$ being an $\mathbb{N} \rightarrow \mathbb{N}$ function.

Any ideas? Thanks in advance!

jroew
  • 11
  • I don't want to sound rude, I'm just curious why you are looking for it . – Asinomás Jun 18 '21 at 16:38
  • 1
    I came to a point, where I end up with t(n) being an exponent in a sum in a problem, that I'm currently working on. The series and product make it rather difficult to proceed. The specific term in question is $\sum^{2^{r+1}-1}{i=2^r} \sum^{i}{j=1} 4^{t(i)}$, which I found empirically to be equal to $(\sum^{r-1}_{t=0} 3 \cdot 2^{r-1-t} \cdot 2^{r-1} \cdot 4^t) + (2^r \cdot 4^r)$. I am struggling with the proof, that these two are in fact equal. – jroew Jun 18 '21 at 16:47
  • 2
    This question has apparently been discussed quite extensively here https://mathoverflow.net/questions/29828/greatest-power-of-two-dividing-an-integer – md5 Jun 18 '21 at 16:48
  • Awesome! That indeed is the same question as mine. Thank you. – jroew Jun 18 '21 at 16:51
  • From the links within the link of @md5 I found, that the problem is called "2-adic valuation of n" or the "ruler sequence", and in fact is an entire field of research. Thanks again for the awesome pointer! – jroew Jun 18 '21 at 17:33

2 Answers2

1

The number of trialing zeros in binary notation is the highest power of $2$ that divides the number in decimal notation.

$t(n)=\displaystyle \sum_{k=1}^\infty \left( 1-\left\lceil\left\{\frac{n}{2^k}\right\}\right\rceil\right)$ satisfies the above condition (where {} denotes the fractional part).

If $k$ is the highest power of $2$ that divides $n$, then the first $k$ terms of $t(n)$ is $1$ while the rest of the terms are $0$ so the formula for $t(n)$ gives the number of trialing zeros in binary notation.

Asher2211
  • 3,406
  • 10
  • 31
  • Hmm.. For $n=4$ I get $t(4) = (1 - \lceil\frac{4}{2}\rceil) + (1 - \lceil\frac{4}{4}\rceil) + (1 - \lceil\frac{4}{8}\rceil) + ... = (1-2) + (1-1) + (1-1) + ... = -1$ (or am I missing something..?) – jroew Jun 18 '21 at 17:11
  • @jroew ${}$ is the denotes the fractional part (eg: ${5.7}=0.7)$ – Asher2211 Jun 18 '21 at 17:13
  • I see, sorry for that. Okay, makes sense now, thank you. Still, I'm not sure if this is easier to handle, than the cosine one above. However, I'll see, if I can work with it, it indeed looks simpler, though. :) – jroew Jun 18 '21 at 17:15
0

What you are trying to compute is $\nu_2(x)$, the $2$-adic valuation of $x$, i.e. the highest exponent $\nu_2(x)$ such that $2^{\nu_2(x)}$ divides $x$ (see here).

If you like a fanciful formula you can get this one:

$$\nu_2(x) = \log_2 \left[x - \sum_{k=0}^{\lfloor \log_2{x} \rfloor}\left(\left\lfloor\frac{2x-1+2^{k+1}}{2^{k+2}}\right\rfloor - \left\lfloor\frac{2x-1+2^{k+2}}{2^{k+3}}\right\rfloor - \left\lfloor \frac{x}{2^{k+2}} \right\rfloor\right)2^k \right] + \frac{1+(-1)^x}{2}$$

For an explanation see here.

  • Thanks for the followup, I guess this floor-heavy formula might be even harder to handle... However, a few days after I posted the original question, I found a way to proove my formula without relying on a direct formula for the 2-adic valuation. As I wrote in a different comment, I tried to simplify a double-sum over a term having the 2-adic valuation in its exponent. Reordering the terms actually did the trick. – jroew Oct 24 '21 at 17:46