{"id":25586,"date":"2017-10-25T20:05:06","date_gmt":"2017-10-25T14:35:06","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25586"},"modified":"2017-10-25T20:05:06","modified_gmt":"2017-10-25T14:35:06","slug":"c-programming-write-efficient-method-check-number-multiple-3-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-write-efficient-method-check-number-multiple-3-2\/","title":{"rendered":"C programming for Write an Efficient Method to Check if a Number is Multiple of 3"},"content":{"rendered":"<p>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\u2019s a multiple of 3. But this solution is not efficient. <span id=\"more-511\"><\/span>You have to get all decimal digits one by one, add them and then check if sum is multiple of 3.<\/p>\n<p>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.<\/p>\n<p>Example: 23 (00..10111)<br \/>\n1) Get count of all set bits at odd positions (For 23 it\u2019s 3).<br \/>\n2) Get count of all set bits at even positions (For 23 it\u2019s 1).<br \/>\n3) If difference of above two counts is a multiple of 3 then number is also a multiple of 3.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>(For 23 it\u2019s 2 so 23 is not a multiple of 3)<\/p>\n<p>Take some more examples like 21, 15, etc\u2026<\/p>\n<pre>Algorithm: isMutlipleOf3(n)\r\n1) Make n positive if n is negative.\r\n2) If number is 0 then return 1\r\n3) If number is 1 then return 0\r\n4) Initialize: odd_count = 0, even_count = 0\r\n5) Loop while n != 0\r\n    a) If rightmost bit is set then increment odd count.\r\n    b) Right-shift n by 1 bit\r\n    c) If rightmost bit is set then increment even count.\r\n    d) Right-shift n by 1 bit\r\n6) return isMutlipleOf3(odd_count - even_count)\r\n<\/pre>\n<p><strong>Proof:<\/strong><br \/>\nAbove 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)<br \/>\nIf difference between sum of odd digits and even digits is multiple of 11 then decimal number is multiple of 11. Let\u2019s see how.<\/p>\n<p>Let\u2019s take the example of 2 digit numbers in decimal<br \/>\nAB = 11A -A + B = 11A + (B \u2013 A)<br \/>\nSo if (B \u2013 A) is a multiple of 11 then is AB.<\/p>\n<p>Let us take 3 digit numbers.<\/p>\n<p>ABC = 99A + A + 11B \u2013 B + C = (99A + 11B) + (A + C \u2013 B)<br \/>\nSo if (A + C \u2013 B) is a multiple of 11 then is (A+C-B)<\/p>\n<p>Let us take 4 digit numbers now.<br \/>\nABCD = 1001A + D + 11C \u2013 C + 999B + B \u2013 A<br \/>\n= (1001A \u2013 999B + 11C) + (D + B \u2013 A -C )<br \/>\nSo, if (B + D \u2013 A \u2013 C) is a multiple of 11 then is ABCD.<\/p>\n<p>This can be continued for all decimal numbers.<br \/>\nAbove concept can be proved for 3 in binary numbers in the same way.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Time Complexity: <\/strong>O(logn)<\/p>\n<p><strong>Program:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%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\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C programming for Write an Efficient Method to Check if a Number is Multiple of 3 &#8211; Mathematical Algorithms &#8211; If sum of digits in a number is multiple of 3.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[74092,74091,74089,74080,72113,72364,70857,73778,72358,70832,73912,74084,74077,74071,74061,74075,74076,74072,74060,69875,74079,74066,70848,70855,74094,74067,74085,74086,74087,74090,74069,74095,70819,74064,74065,72110,74082,74081,74083,74088,74070,74093,74059,70826,74063,74074,74073,74062,74078,74068],"class_list":["post-25586","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-5-examples-of-programming-language","tag-basic-programming-interview-questions","tag-c-coding-questions","tag-c-program-for-fibonacci-series","tag-c-program-for-prime-number","tag-c-program-prime-number","tag-c-program-to-add-two-numbers","tag-c-program-to-find-power-of-a-number-using-function","tag-c-program-to-find-prime-number","tag-c-programming-codes","tag-c-programming-problems","tag-coding-interview-questions","tag-coding-problems","tag-coding-test-questions","tag-common-programming-interview-questions","tag-composite-numbers-from-1-to-1000","tag-fibonacci-c-program","tag-fibonacci-program-in-c","tag-fibonacci-series-c-program","tag-find-hcf","tag-first-six-multiples-of-3","tag-for-loop-c-programming","tag-for-loop-in-c-programming","tag-for-loop-in-c-programming-example","tag-gcd-in-c","tag-gcd-program-in-c","tag-hcf-lcm-of-numbers","tag-hcfc","tag-how-to-write-a-program-in-c","tag-how-to-write-a-program-in-c-language","tag-if-c-programming","tag-interview-programming-questions","tag-introduction-to-c-programming","tag-numbers-divisible-by-7","tag-perfect-number-in-c","tag-prime-number-program-in-c","tag-program-for-prime-number-in-c","tag-programming-interview-questions","tag-programming-questions","tag-programming-questions-in-c","tag-programming-test-questions","tag-smallest-even-number","tag-smallest-three-digit-number","tag-wap-in-c","tag-what-is-fizz","tag-while-loop-in-c","tag-write-a-program-to-find-gcd-of-two-numbers","tag-write-ac-program-for-finding-gcd-of-two-given-numbers","tag-write-ac-program-to-find-gcd-of-two-numbers","tag-write-ac-program-to-find-lcm-of-two-numbers"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25586"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25586\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}