som
som

Reputation: 491

Bit programming

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.

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..?

Upvotes: 3

Views: 246

Answers (2)

Shahbaz
Shahbaz

Reputation: 47523

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:

g + 10*f + 10^2*e + 10^3*d + 10^4*c + 10^5*b + 10^6*a

As we know, (a+b)%c is equal to (a%c+b%c)%c and (a*b)%c is 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

    (g + 10*f + 10^2*e + 10^3*d + 10^4*c + 10^5*b + 10^6*a)%2 =
    (g%2 + 0 + 0 + 0 + 0 + 0 + 0)%2 =
    g%2
    

    therefore, a number is divisible by 2, if its last digit is divisible by 2

  • 3

    (g + 10*f + 10^2*e + 10^3*d + 10^4*c + 10^5*b + 10^6*a)%3 =
    (g%3 + f%3 + e%3 + d%3 + c%3 + b%3 + a%3)%3 =
    ... repeat operation for this number
    

    therefore, a number is divisible by 3, it the sum of its digits is divisible by 3

  • 4

    (g + 10*f + 10^2*e + 10^3*d + 10^4*c + 10^5*b + 10^6*a)%4 =
    (g%4 + 2*f%4 + 0 + 0 + 0 + 0 + 0)%4 =
    ... repeat if bigger than 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

    (g + 10*f + 10^2*e + 10^3*d + 10^4*c + 10^5*b + 10^6*a)%5 =
    (g%5 + 0 + 0 + 0 + 0 + 0 + 0)%5 =
    g%5
    

    therefore, a number is divisible by 5, if its last digit is either 0 or 5

  • ...

  • 11

    (g + 10*f + 10^2*e + 10^3*d + 10^4*c + 10^5*b + 10^6*a)%11 =
    (g%11 - f%11 + e%11 - d%11 + c%11 - b%11 + a%11)%11 =
    (g - f + e - d + c - b + a)%11 =
    ... repeat operation for this number
    

    (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 abcdefg evaluates to:

g + 2*f + 2^2*e + 2^3*d + 2^4*c + 2^5*b + 2^6*a

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 simply digit. That doesn't change the methodology at all.

Let's see the remainder of our number by

  • 2

    (g + 2*f + 2^2*e + 2^3*d + 2^4*c + 2^5*b + 2^6*a)%2 =
    (g + 0 + 0 + 0 + 0 + 0 + 0)%2 =
    g
    

    therefore, a number is divisible by 2, if its last digit is 0

  • 3

    (g + 2*f + 2^2*e + 2^3*d + 2^4*c + 2^5*b + 2^6*a)%3 =
    (g - f + e - d + c - b + a)%3 =
    ... repeat operation for this number
    
  • 4

    (g + 2*f + 2^2*e + 2^3*d + 2^4*c + 2^5*b + 2^6*a)%4 =
    (g + 2*f + 0 + 0 + 0 + 0 + 0)%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

    (g + 2*f + 2^2*e + 2^3*d + 2^4*c + 2^5*b + 2^6*a)%5 =
    (g + 2*f - e - 2*d + c + 2*b - a)%5 =
    ... repeat operation for this number
    
  • and so on

Upvotes: 5

Forgottn
Forgottn

Reputation: 563

As there no general way to find out if a number is divisible by another number (see here for example), there is obviously no way to find it out in binary representation.

Upvotes: 0

Related Questions