Is there any general method or particular methods for different numbers through which we can get to know that a given number n in its binary representation is divisible by another number m?
For example:
n=23 (00010111)
m=3
The number is divisible by 3 if the difference between the count of bits set at even and odd positions is divisible by 3.
- bits set to 1 at even position = 1
- bits set to 1 at odd position = 3
So 3 - 1 = 2 is not divisible by 3, and therefore 23 is not divisible by 3.
I want to ask if there are other methods to find if a number is divisible by 2, 4, 5, 6, 7, etc..?
You can’t find a simple rule for all of them. Here is the idea of how such rules are created.
Let’s first talk about base 10. Imagine the number
abcdefg. This number is in fact:As we know,
(a+b)%cis equal to(a%c+b%c)%cand(a*b)%cis equal to((a%c)*(b%c))%c(you can learn better about these properties having known congruence)So, let’s see the remainder of our number by:
2
therefore, a number is divisible by 2, if its last digit is divisible by 2
3
therefore, a number is divisible by 3, it the sum of its digits is divisible by 3
4
therefore, a number is divisible by 4, if its last digit plus two times its one before last digist is divisible by 4
5
therefore, a number is divisible by 5, if its last digit is either 0 or 5
…
11
(Note that 10%11 could be seen as -1 (they are congruent))
and so on!
As you can see, in base 10, remainder by 11 gave you the same formula as remainder by 3 in base 2. This is not a coincidence.
Now let’s assume our number is in base 2. Therefore
abcdefgevaluates to:The method to find the formulae is exactly as above. The only thing that makes it simpler here is that if the divisor is bigger than 1, then remainder of all digits with the divisor is the digit itself (because the digit is only 0 or 1), so all the
digit%divisors become simplydigit. That doesn’t change the methodology at all.Let’s see the remainder of our number by
2
therefore, a number is divisible by 2, if its last digit is 0
3
4
therefore, a number is divisible by 4, if its last digit plus two times its one before last digist is divisible by 4
5
and so on