6

I always find myself doing tests with binary numbers (without a calculator, I'm now developing automatas) and I've always asked myself if there was a fast trick to check whether a generic number is divisible by another binary number.

Let's say I need to test if 100111011 is divisible by 3 or by 11, is there a "common" way to deal with these kind of problems? Or checking one by one is the only possible way to do it?

I know that to check if a number is divisible by 3 you have to look for a certain thing, by 11 you have to look for some other things, is that really the fastest and easiest way?

Matt
  • 247
  • 3
    Say you have to check divisibility by $k$. You have to compute the powers of $2 \pmod{k}$: after a certeain point they start repeating. Then every $1$ in your binary string has an assigned value $\pmod{k}$: you need that the sum of those numbers is $0 \pmod{k}$. – Crostul Apr 10 '17 at 18:59
  • @Crostul That sums up everything. Related : https://math.stackexchange.com/questions/1165131/division-rules-for-other-number-systems?noredirect=1&lq=1 – user471651 Sep 28 '23 at 12:22

1 Answers1

10

I'm going to start with an example for how to derive divisibility rules for base 10 first because I feel it's easier to understand. Suppose we have $n$ which is divisible by $7$ and we want to prove it, write it in the form $n=10k+j$ so $j$. Notice that $j$ is the last digit in the decimal expansion. Now here's the trick, because $7\vert n$ we know $7\vert (n-21j)$ so $7\vert 10(k-2j)$. $10$ and $7$ are relatively prime so we get that $7\vert (k-2j)$. Put in words we can take the last digit from $n$ and subtract twice the quantity from the rest of the digits of $n$ then our new number is divisible by 7 if and only if $n$ was. For example let's check $252$: $$252 \to 25 - 2 \times 2 = 21 \to 2 - 2 \times 1 = 0.$$ Note that you don't have to reach $0$, we could have stopped at $21$ if we recognized it's divisible by $7$.

Now we can use the same trick to come up with divisibility rules for base 2. Write $n$ in the form $$n=2k+j$$ and assume $3\vert n$. Now we have $n-3j=2(k-j)$ which is also divisible by 3, so $3\vert (k-j)$. This rule is really nice because it ends up being reduced to the alternating sum. So if the alternating sum of the binary expansion of $n$ is divisible by 3 then $n$ is. For example let's check $228;11100100$ $$ \matrix{+&-&+&-&+&-&+&-\\1&1&1&0&0&1&0&0\\ \hline \\1&-1&+1 & & & -1 & & & =0} $$ So we see $228$ is divisible by $3$.

For divisibility by $11$ we write $n+11j=2(k+6j)$ so $11\vert(k+6j)$ if $11\vert n$. The most efficient way I can come up with to use this is to take the binary expansion of $n$, cut off all trailing $0$s and the last $1$ and add $110$ ($6$). For example $2992;101110110000$, I put vertical bars to show where I cut the number off: $$1011101\vert10000\to 1011101+110=110001\vert 1\to 110001+110=11011\vert 1 \to$$ $$11011+110=10000\vert 1\to 10000+110=101\vert 10\to 101+110=1011$$ We reached the binary expansion for $11$ so we have shown $2992$ to be divisible by 11.

Other rules can be derived using the same trick.

  • Thanks, but why does it "reduce to the alternating sum"? For $n = 15$ which is $1111$ in binary I would have $k-j = 111 - 1 = 111 = 7$. And three doesn't divide into this.

    So I'm not following how this reduces to the alternating sum.

    – baxx Jul 24 '18 at 10:11
  • @baxx Negative. You have a minor slip in the arithmetic. $111-1=110_2=6_{10}$ and $3|6$. The alternating sum of $15_{10}=1111_2$ is $+1-1+1-1=0$ and $3|0$ trivially. – Cosmic Ossifrage Mar 07 '21 at 15:50
  • How did you arrive at 'alternating sum' from 3|k-j? Following that, 5|k-3j. Can this be reduced to something of that form? – Sourav Kannantha B Feb 18 '23 at 08:47
  • Actually, 5|k-j in base 4. So can we reduce that to alternating sum in base 4? – Sourav Kannantha B Feb 18 '23 at 08:49