{"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=&#8221;banner&#8221;]\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=&#8221;banner&#8221;]\n<p><strong>Time Complexity: <\/strong>O(logn)<\/p>\n<p><strong>Program:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/> <br\/>\/* Fnction to check if n is a multiple of 3*\/<br\/>int isMultipleOf3(int n)<br\/>{<br\/>    int odd_count = 0;<br\/>    int even_count = 0;<br\/> <br\/>    \/* Make no positive if +n is multiple of 3<br\/>       then is -n. We are doing this to avoid<br\/>       stack overflow in recursion*\/<br\/>    if(n &lt; 0)   n = -n;<br\/>    if(n == 0) return 1;<br\/>    if(n == 1) return 0;<br\/> <br\/>    while(n)<br\/>    {<br\/>        \/* If odd bit is set then<br\/>           increment odd counter *\/<br\/>        if(n &amp; 1) <br\/>           odd_count++;<br\/>        n = n&gt;&gt;1;<br\/> <br\/>        \/* If even bit is set then<br\/>           increment even counter *\/<br\/>        if(n &amp; 1)<br\/>            even_count++;<br\/>        n = n&gt;&gt;1;<br\/>    }<br\/> <br\/>     return isMultipleOf3(abs(odd_count - even_count));<br\/>}<br\/> <br\/>\/* Program to test function isMultipleOf3 *\/<br\/>int main()<br\/>{<br\/>    int num = 23;<br\/>    if (isMultipleOf3(num))    <br\/>        printf(&quot;num is multiple of 3&quot;);<br\/>    else<br\/>        printf(&quot;num is not a multiple of 3&quot;);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}