Possible Duplicate:
Check if a number is divisible by 3
Is it true that a binary number is divisible by 3 iff it has an even number of ones? like 11000 is divisible by 3 whereas 1110 is not.
Basically count the number of non-zero odd positions bits and non-zero even position bits from the right. If their difference is divisible by 3, then the number is divisible by 3. For example: 15 = 1111 which has 2 odd and 2 even non-zero bits.
So to check if a number is divisible by 3, you need to determine if dividing the number by three has a remainder of zero. var number = 21; if( number % 3 == 0) { //The number is divisible by three.
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 the difference between the above two counts is a multiple of 3 then the number is also a multiple of 3.
Consider the number 4368. We know that 9, 99, 999,… are divisible by 3, and thus the multiples of these numbers are also divisible by 3. So, the divisibility of 4368 is now dependent on the sum 4 + 3 + 6 + 8. Here, 4, 3, 6 and 8 are the digits of the number 4368.
No - there is a trick but it's a little more complicated than that - you have to count the number of 1s at even positions and the number of 1s at odd positions. See e.g. Check if a number is divisible by 3.
No, that's wrong. For example 5_dec = 101_bin is not divisble by 3. To check for divisbility by three, you have to count the number of ones in even position and substract the number of ones in odd positions. If the difference is divisble by three, the original number is divisbilble by three (which, in turn, can be checked by reiterating the same rule).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With