The very first solution that comes to our mind is the one that we learned in school. If sum of digits in a number is multiple of 3 then number is multiple of 3 e.g., for 612 sum of digits is 9 so it’s a multiple of 3. But this solution is not efficient. You have to get all decimal digits one by one, add them and then check if sum is multiple of 3.

There is a pattern in binary representation of the number that can be used to find if number is a multiple of 3. If difference between count of odd set bits (Bits set at odd positions) and even set bits is multiple of 3 then is the number.

Example: 23 (00..10111)
1) Get count of all set bits at odd positions (For 23 it’s 3).
2) Get count of all set bits at even positions (For 23 it’s 1).
3) If difference of above two counts is a multiple of 3 then number is also a multiple of 3.

[ad type=”banner”]

(For 23 it’s 2 so 23 is not a multiple of 3)

Take some more examples like 21, 15, etc…

Algorithm: isMutlipleOf3(n)
1) Make n positive if n is negative.
2) If number is 0 then return 1
3) If number is 1 then return 0
4) Initialize: odd_count = 0, even_count = 0
5) Loop while n != 0
    a) If rightmost bit is set then increment odd count.
    b) Right-shift n by 1 bit
    c) If rightmost bit is set then increment even count.
    d) Right-shift n by 1 bit
6) return isMutlipleOf3(odd_count - even_count)

Proof:
Above can be proved by taking the example of 11 in decimal numbers. (In this context 11 in decimal numbers is same as 3 in binary numbers)
If difference between sum of odd digits and even digits is multiple of 11 then decimal number is multiple of 11. Let’s see how.

Let’s take the example of 2 digit numbers in decimal
AB = 11A -A + B = 11A + (B – A)
So if (B – A) is a multiple of 11 then is AB.

Let us take 3 digit numbers.

ABC = 99A + A + 11B – B + C = (99A + 11B) + (A + C – B)
So if (A + C – B) is a multiple of 11 then is (A+C-B)

Let us take 4 digit numbers now.
ABCD = 1001A + D + 11C – C + 999B + B – A
= (1001A – 999B + 11C) + (D + B – A -C )
So, if (B + D – A – C) is a multiple of 11 then is ABCD.

This can be continued for all decimal numbers.
Above concept can be proved for 3 in binary numbers in the same way.

[ad type=”banner”]

Time Complexity: O(logn)

Program:

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%20%0A%2F*%20Fnction%20to%20check%20if%20n%20is%20a%20multiple%20of%203*%2F%0Aint%20isMultipleOf3(int%20n)%0A%7B%0A%20%20%20%20int%20odd_count%20%3D%200%3B%0A%20%20%20%20int%20even_count%20%3D%200%3B%0A%20%0A%20%20%20%20%2F*%20Make%20no%20positive%20if%20%2Bn%20is%20multiple%20of%203%0A%20%20%20%20%20%20%20then%20is%20-n.%20We%20are%20doing%20this%20to%20avoid%0A%20%20%20%20%20%20%20stack%20overflow%20in%20recursion*%2F%0A%20%20%20%20if(n%20%3C%200)%20%20%20n%20%3D%20-n%3B%0A%20%20%20%20if(n%20%3D%3D%200)%20return%201%3B%0A%20%20%20%20if(n%20%3D%3D%201)%20return%200%3B%0A%20%0A%20%20%20%20while(n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20If%20odd%20bit%20is%20set%20then%0A%20%20%20%20%20%20%20%20%20%20%20increment%20odd%20counter%20*%2F%0A%20%20%20%20%20%20%20%20if(n%20%26%201)%20%0A%20%20%20%20%20%20%20%20%20%20%20odd_count%2B%2B%3B%0A%20%20%20%20%20%20%20%20n%20%3D%20n%3E%3E1%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20If%20even%20bit%20is%20set%20then%0A%20%20%20%20%20%20%20%20%20%20%20increment%20even%20counter%20*%2F%0A%20%20%20%20%20%20%20%20if(n%20%26%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20even_count%2B%2B%3B%0A%20%20%20%20%20%20%20%20n%20%3D%20n%3E%3E1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%20return%20isMultipleOf3(abs(odd_count%20-%20even_count))%3B%0A%7D%0A%20%0A%2F*%20Program%20to%20test%20function%20isMultipleOf3%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20num%20%3D%2023%3B%0A%20%20%20%20if%20(isMultipleOf3(num))%20%20%20%20%0A%20%20%20%20%20%20%20%20printf(%22num%20is%20multiple%20of%203%22)%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20printf(%22num%20is%20not%20a%20multiple%20of%203%22)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/] [ad type=”banner”]