5
$\begingroup$

If you reverse the binary digits of a multiple of $3$, the result is always a multiple of $3$. The same is not true for $7$, but it does appear to happen more often than $\frac{1}{7}$ of the time. For fun, I'll call a number $n$ such that the reverse of the binary digits of $7n$ represents a multiple of $7$ a "sevenly" number.

I wrote a simple Python program to compute the frequency of sevenly numbers, and after letting it run for an hour the proportion had been decreasing overall, but not very steadily, and it had yet to fall below $0.25$.

I was wondering if someone knew how to determine the asymptotic density of sevenly numbers. My intuition says it should be $\frac{1}{7}$, but the computations I've done seem to suggest it might not.

EDIT

My original program didn't tell me what number it was on, only the proportion, but I ran it for an hour, so it probably got into the millions. I've changed the program since then to print out the number it's on, too. Here is that code:

def brev(n):
    return int("0b"+(bin(n)[::-1])[:-2],2)

n = 1
sevenlies = 0
while True:
    if brev(7*n)%7 == 0:
        sevenlies += 1
    print("%d\t%f"%(n,sevenlies/n))
    n += 1
  • 0
    Has it fallen below $2/7\approx0.28$?2017-02-10
  • 0
    @alex.jordan Yeah, I was thinking that too ... could it have something to do with the binary number ending in a 0 or 1 ... and if it ends in a 0, the reversal takes away the 0? ... but that still doesn;t make it clear why it would be 2/7 ...2017-02-10
  • 0
    It did fall below $\frac{2}{7}$. I don't recall if it stayed below $\frac{2}{7}$ for very long. I terminated the program shortly after that time anyway.2017-02-10
  • 0
    @rayradjr How high up did you get? I mean in terms of $n$?2017-02-10
  • 0
    My guess is that we can go with a probabilistic approach. If you pick every binary digit of a number randomly, keeping track of its value mod $7$ and of the reversed number mod $7$ is doing a Markov chain on $49$ states. Now we can look at its stationary distribution and watch what happens if we look only at the states where the number is a multiple of $7$.2017-02-10
  • 0
    hmm it gives a completely uniform distribution so... strange.2017-02-10

3 Answers 3

1

Here is my reasoning on why the number of times the remainders mod $7$ match more than randomly.

The key things, to me, are that mod $7$, $2^{3m} \equiv 1, 2^{3m+1} \equiv 2, 2^{3m+2} \equiv 4 $.

Therefore, if $m$ is one less than the number of bits in $n$, write $n = \sum_{k=0}^m b_k 2^k $.

If $s(n)$ is the value of $n$ mod $7$ and $d(0, 1, 2) = (1, 2, 4)$, then

$s(n) \equiv \sum_{k=0}^m b_k d(k \bmod 3) $

Reversing the bits, define $r(n) =\sum_{k=0}^m b_{m-k} 2^k =\sum_{k=0}^m b_{k} 2^{m-k} $.

Then, mod $7$,

$\begin{array}\\ s(r(n)) &=\sum_{k=0}^m b_{k} 2^{m-k}\\ &\equiv\sum_{k=0}^m b_{k} d(m-k \bmod 3)\\ \end{array} $

so that

$\begin{array}\\ s(r(n))-s(n) &\equiv\sum_{k=0}^m b_{k} (d(m-k \bmod 3)-d(k \bmod 3))\\ \end{array} $

Whenever $d(m-k \bmod 3) =d(k \bmod 3) $, the term is zero. This happens when $m-k \equiv k \bmod 3$ or $m \equiv 2k \bmod 3$ or $2m \equiv k \bmod 3$.

This happens about one third of the time so that, independent of the value, one third of the bits will cancel out.

  • 0
    So if I understand you correctly, pairings like 5 and 7 (where the bistrings of 35 and 49 are each others's reverse) and 19 and 23 (where 133 and 161 are each others reverse) are bound to happen with a frequency that would force the proportion of sevenly numbers to be greater than $\frac{1}{7}$? I can certainly believe that, but I still don't quite see that. Sorry, I'm not the best mathematician, but as you can see from my post I have been thinking about this problem quite a bit so would like to understand!2017-02-11
  • 0
    25 and 35 are another pair ...and so are 37 and 55... They do seem to occur quite frequently ...2017-02-11
0

I have a suspicion that eventually this proportion will go down to where you intuit it should be, which is $\frac{1}{7}$, but that it is initially a good bit higher that, and that it wil take quite a while to converge to this value.

I have no proof for this, but I do have my reasons:

First let's note that for small numbers, you have a disproportionately high number of 'sevenly' numbers: Indeed, all numbers 1 through 10 are 'sevenly'!

Second, while some of this is because of the nature of the bit string $111$ for 7, at the same time some of this is 'coincidence'. For example, 1 is sevenly for a good reason, because the reverse of $111$ is $111$ itself ... 1 would not be 'sixly' for instance. But 3 is sevenly because the bit string for 21 (10101) happens to be symmetrical as well, and the bit strings for 35 and 49 happen to be each others' reverse, making both 5 and 7 sevenly. And finally, any number whose bit string is $1(000^*1)^*$ will be sevenly, and 9 is like that (and so are 17, 33, 65, 73 ...). So again, I think the high proportion of sevenly numbers at the beginning is a combination of $111$ being symmetrical as well as pure happenstance.

Third, if $x$ is sevenly, then $2x$ will be sevenly as well, since the bit string for $7*2x$ will of course just be the bit string for $7x$ with a 0 after it, and that extra 0 gets dropped when the string is reversed.

Likewise, if $x$ is evenly $8x+1$ is evenly as well, since the bit string for $8x+1$ is the bitstring for $x$ followed by $001$ and a little thought shows that multiplying by $7$ and then reversing amounts to adding $111$ in front of the reversed bit string for $7x$ which we already know represents a multiple of $7$, so the reversed bit string for $8x+1$ will represent a multiple of $7$ as well.

Hence, because 1 is sevenly, so are 2,4,8,16, etc. And because 3 is sevenly, 6, 12, 24, etc. are sevenly as well. And with 5 we get 10, 20, etc. Likewise, because 1 is sevenly, so is 9, because 2 is sevenly, so is 17, because 3 is sevenly, so is 25, etc.

In other words, the extremely high proportion of sevenly numbers at the beginning will propagate to the higher numbers through these kinds of connections between the sevenly numbers.

However, this high initial proportion will not propagate perfectly. That is, as we keep multiplying the sevenly numbers by 2 to get new sevenly numbers, gaps will start to appear ( as we see with 11,13, and 15, which are the first non-sevenly numbers). Thus, the proportion will go down. Then again, some of these gaps will be filled again by some 'new' kind of sevenly number (the pairing of 19 and 23 is a good example ... And so is 27 which maps to itself) and this will slow down the gradual decrease of the proportionality. So it may take a good while before the proportion will approach $\frac{1}{7}$.

Nevertheless: I am thinking that if you start with 'large enough' bitstrings, this initial disproportionality will gradually disappear, in that if you have a very large bitstring to begin with, the resulting bitstring is likely to be very large as well, and thus one would expect the resulting bit strings to be more 'random' again, making the proportion approach $\frac{1}{7}$ again.

So: could you maybe start your program with a large number, e.g $n = 1,000,000$? and see what proportion you get from that point on?

EDIT

Marty's analysis is interesting. In my eyes, what it shows in particular is that if you focus on bit strings whose length is a multiple of 3, and where the last digit is a 1, then if that bit string represents a multiple of 7, then it is, at least for 'smaller sized' bit strings, significantly more likely than $\frac{1}{7}$ that the reverse bit string is a multiple of 7 as well, thus accounting for the higher proportion of sevenly numbers. And this is because for smaller size bit strings, it is fairly likely to get just as many 1's in the 3k positions as in the 3k+1 positions, and in that case we are dealing with a sevenly number. Indeed, we run into exactly this case with the aforementioned pairing of 5 and 7, but we also have such pairings for 37 and 55, for 53 and 59, for 45 and 63, and for 57 and 69 ... No wonder we got so many sevenly numbers among the first 70 numbers!

Moreover, these kinds of pairings will happen with a probability of more than $\frac{1}{7}$ for quite some time. For bit strings of length 18, the probability of getting such pairings is about 0.25, and even for bit strings of length 30 this happens in about 18% of the cases!

This means that we will see disproportionaly more sevenly numbers for numbers at least on the order of 1,000,000,000, and in combination with the earlier mentioned propagation effects, the proportion will likely remain significantly above $\frac{1}{7}$ for a long, long time, possibly well into the quadrillions. Still, I predict that, though very persistent, even this effect will eventually fade away.

EDIT 2

OK, I generated random bit strings of length 99 and used Marty's method of figuring out whether that represented a number (which is on the order of $10^30$!) that is a multiple of 7: Just let $x$ be the number of 1's in the 3k+1 positions (i.e. the 1st, 4th, 7th, etc. bit position), $y$ the number of 1's in the 3k+2 positions, and $z$ the number of 1's in the $3k$ bit positions, and the number is divisible iff $4x+2y+z$ is divisible by 7. Of those that were divisible by 7, I looked whether the reverse bit string represented a number divisible by 7, and in what proportion. I found ... (drum roll) ... 0.14!

This is no proof, but certainly confirms my above stated suspicions that the proportion does indeed eventually go down to $\frac{1}{7}$, but that it's just that for small numbers, the proportion is higher for reasons already stated.

  • 0
    This doesn't take into accout that the binary numbers being reversed is a multiple of $7$ to begin with.2017-02-10
  • 0
    @Servaes Yeah, I had already realized my explanation doesn't work ... :(2017-02-10
  • 0
    I was dumb and didn't have the program tell me what number I was at at any given time, but I ran it for an hour so I'd guess it got to at least a million. I'll add the code to the question (but I'll change it to print out the number it's at too).2017-02-10
  • 0
    @rayradjr Just to confirm: if $n= 6$, then you take $6*7=42$, which is bitstring $101010$, which then becomes $010101$, which is 21 ... which is divisible by 7 ... and thus 6 is 'sevenly', right?2017-02-10
  • 0
    @Bram28 yeah, that's right2017-02-10
  • 0
    @rayradjr I wonder what would happen if you start with $n$ being 1 million or so, i.e. avoid the small numbers completely. Can you try that?2017-02-10
  • 0
    @Bram28 It's at $0.235008$ at $8217933$ (not counting the first million numbers). I didn't check to see if it was varying much. In the original run, I noticed it would stay fairly constant for long periods of time and then suddenly change a bunch.2017-02-10
  • 0
    @rayradjr Huh! Thanks for running that! So ... On the one hand that 'stopping' and 'going' behavior might be a reflection of the differences in density being propagated through the numbers (e.g. $x \to 2x \to 4x...$... and maybe there are other such systematic relationships, but on the other hand: that is really far off from 1/7!! Too far off for my explanation to work ... I'll have to think bout this some more ...2017-02-11
  • 0
    @rayradjr Oh, did the proportion at least seem to go down, little as it did?2017-02-11
  • 0
    Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/53488/discussion-between-bram28-and-rayradjr).2017-02-12
  • 0
    @rayradjr marty's answer makes me believe that the disproportionally high number of sevenly numbers in the beginning will actually last for quite a while ... Is it possible to start at, say, 1,000,000,000?2017-02-12
  • 0
    @rayradjr also, just to confirm a suspicion: is the proportion of sevenly numbers higher between around 2.5 million and 5 million than from 1 to 2.5 million and from 5 million to the 8 million you got to?2017-02-12
  • 0
    @rayradjr See my latest edit ... I have good confirmation that the proportion will indeed eventually go down to $\frac{1}{7}$ ... you just need to work with really big numbers to see that!2017-02-13
0

If the ratio converges to a limit, then it is $1/7$.

Let $n'$ be the reverse of $n$.
If $n$'s binary expansion is $\sum_{k=1}^m d_k 2^k$, then, modulo $7$, we have
$n' = \sum_{k=1}^m d_k 2^{m-k} \equiv \sum_{k=1}^m d_k 2^{m-k} 8^k \equiv 2^m \sum_{k=1}^m d_k 4^k$,
and so $n'$ is a multiple of $7$ if and only if $\sum_{k=1}^m d_k 4^k \equiv 0$,
which means that $n$'s binary string, when read in base $4$, is a multiple of $7$.

Let $a_n$ be the number of sevenly numbers between $0$ and $2^n/7$, so the number of $n$-digits binary strings that are multiples of $7$ in both base $2$ and base $4$.

$(a_n)_{n \ge 0} = 1,1,1,2,3,5,10,16,28,56,\ldots$

If you let $a^{i,j}_n$ be the number of $n$-digits strings that represent, mmodulo $7$, $i$ when read in base $2$ and $j$ when read in base $4$, you have the recurrence relations $a^{i,j}_{n+1} = a^{4i,2j}_n + a^{4i+3,2j+5}_n$ and so if you put all those numbers in a vector $V_n \in \Bbb R^{49}$ there is a matrix $M$ such that $V_{n+1} = M V_n$.

The characteristic polynomial of $M$ is
$(x-2)(x^3-1)^4(x^9 - 10x^6 + 24x^3 - 8)^2(x^9 -3x^6 - 4x^3 - 1)^2$

$2$ is its largest eigenvalue, and its eigenspace is generated by the uniform vector $(1,1,1,\ldots,1)$, so as $n \to \infty$, the components of $V_n$ are all equivalent to $\frac 1 {49} 2^n$, and so as $n$ gets larger, $a_n = a_n^{0,0} \sim \frac 1 {49} 2^n$ which is $\frac 17$ of the $\frac 17 2^n$ numbers we are looking at.