4

Consider powers of 10 written as binary numbers.

10 = 1010 , 100 = 1100100 , 1000 = 1111101000

How can I find a formula for the number of zeroes between the last and the second last 1?

Stefan Perko
  • 12,467
  • You do know that you may concentrate on powers of $5$, right? Then you won't have to deal with the trailing zeroes. – Arthur Jan 15 '16 at 16:23
  • One may as well compute the same for the powers $5^k$ of $5$, whose binary expressions are the same as those for $10$, but with the $k$ zeros at the end of the sequence truncated. – Travis Willse Jan 15 '16 at 16:23
  • 3
    So it's one less than the greatest power of $2$ which exactly divides $5^n-1$. – lulu Jan 15 '16 at 16:25
  • @Jack Wother: Your comment shows that you have not understood the first two comments! Look at powers of $5$ in binary, and you will see what they mean. – TonyK Jan 15 '16 at 19:05

2 Answers2

3

As described in the comments: the answer, let's call it $f(n)$, is one less than the greatest power of $2$ which divides $5^n-1$. Thus $$f(n)=v_2(5^n-1)-1$$ where, as usual, $v_2(k)$ denotes the greatest power of $2$ dividing $k$.

To compute $v_2(5^n-1)$...

first note that this is $2$ if $n$ is odd: indeed $5^n-1=(5-1)(5^{n-1}+\dots+1)$ regardless of the parity of $n$ and, if $n$ is odd the second factor is easily seen to be odd.

If $n=2k$ is even then $$5^n-1=5^{2k}-1=(5^k-1)(5^k+1)$$ Now, the second factor here is even (clearly) but is never divisible by $4$ (as $5^k=1$ mod($4$)). It follows that $$v_2(5^{2k}-1)=v_2(5^k-1)+1$$

Combining these remarks one sees that, for $n=2^km$ with $m$ odd we have $$v_2(5^{2^km}-1)=2+k$$

thus, the final answer is $$f(n)=f(2^km)=k+1$$

lulu
  • 70,402
  • One might want to look it up on OEIS, too. – Ivan Neretin Jan 15 '16 at 17:27
  • Thank you for replying. There is one thing that I do not understand is that why would the function be f(n)=v2(5n−1)−1 as defined? Many thanks once again – Jack Wother Jan 15 '16 at 18:42
  • 1
    No problem. This is equivalent to what appears in the posted solution of @YvesDaoust . Starting, as you did, with $10^n$, you kill off the trailing $0's$. That's equivalent to dividing by $2^n$, which gets you to $5^n$. Now $5^n$ is clearly odd, so its binary expansion ends in $1$. I remove that $1$ by subtracting it off, he removes it by dividing by $2$ and discarding the fractional remainder. Sticking with my subtraction, you now want the number of trailing $0's$ in $5^n-1$, less one because you don't count the right most slot (that was taken up by the final $1$ before subtraction). – lulu Jan 15 '16 at 18:53
  • Continuing...the number of training $0's$ in the binary expansion of $m$ is precisely $v_2(m)$ so we are done. – lulu Jan 15 '16 at 18:54
  • Thank you so much. You have been fantastic. I understand it completely now. – Jack Wother Jan 15 '16 at 19:06
0

This is the number of trailing zeroes at the end of $5^p/2$ (integer division). In mathematical terms, it is the multiplicity of the factor $2$ in the prime decomposition of this number.

In programming, you will shift right until you get an odd number, and count the shifts.