Write an Efficient Method to Check if a Number is Multiple of 3(快速判断3的倍数)
2014-03-12 21:37
465 查看
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.
(For 23 it’s 2 so 23 is not a multiple of 3)
Take some more examples like 21, 15, etc…
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.
(For 23 it’s 2 so 23 is not a multiple of 3)
Take some more examples like 21, 15, etc…
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.
Time Complexity: O(logn)
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.
(For 23 it’s 2 so 23 is not a multiple of 3)
Take some more examples like 21, 15, etc…
Write an Efficient Method to Check if a Number is Multiple of 3
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.
(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.
Time Complexity: O(logn)
#include<cstdio> #include<cstdlib> #include<algorithm> /*a fast way to check a number is a multiple of 3*/ bool ismultiple(long long n) { int odd=0,even=0; if(n<0) n=-n; if (n==0) return 1; if(n==1) return 0; while(n--) { if(n&1) odd++; n=n>>1; if(n&1) even++; n=n>>1; } return abs(odd-even); } int main() { long long n; while(scanf("%lld",&n)!=EOF) { if(ismultiple(n)) printf("yes\n"); else printf("no\n"); } }
相关文章推荐
- Write an Efficient Method to Check if a Number is Multiple of 3
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Given two strings s and t, write a function to determine if t is an anagram of s.
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- Write an Efficient C Program to Reverse Bits of a Number
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Given an integer, write a function to determine if it is a power of two
- Check if the key is composed of an arbitrary number of concatenations of strings from the dictionar
- 1.3 Given two strings, write a method to decide if one is a permutation of the other.
- How do I check if a type is a subtype OR the type of an object?
- Simple way to check if an image bitmap is blur
- 9. 13. 6. Stack基本用法 To find out if an element is on the stack: the search() method
- use isSubstring to check if one word is a rotation of another.
- Write a method to print the last K lines of an input file using C++
- An effective way to use pattern to instead of multiple if else statements
- How to check if a Number is Positive or Negative in Java - Interview Question
- Adobe--Without using /,% ,+and * operators. write a function to check whether a number is divisible by 3 or nor?
- 判断一个整数是否为回文数 Check if a number is palindrome