Check if a number is divisible by 3

后端 未结 16 2822
予麋鹿
予麋鹿 2020-12-01 01:30

I need to find whether a number is divisible by 3 without using %, / or *. The hint given was to use atoi() function. Any

相关标签:
16条回答
  • 2020-12-01 01:59
    bool isDiv3(unsigned int n)
    {
        unsigned int n_div_3 =
            n * (unsigned int) 0xaaaaaaab;
        return (n_div_3 < 0x55555556);//<=>n_div_3 <= 0x55555555
    
    /*
    because 3 * 0xaaaaaaab == 0x200000001 and
     (uint32_t) 0x200000001 == 1
    */
    }
    
    bool isDiv5(unsigned int n)
    {
        unsigned int n_div_5 =
            i * (unsigned int) 0xcccccccd;
        return (n_div_5 < 0x33333334);//<=>n_div_5 <= 0x33333333
    
    /*
    because 5 * 0xcccccccd == 0x4 0000 0001 and
     (uint32_t) 0x400000001 == 1
    */
    }
    

    Following the same rule, to obtain the result of divisibility test by 'n', we can : multiply the number by 0x1 0000 0000 - (1/n)*0xFFFFFFFF compare to (1/n) * 0xFFFFFFFF

    The counterpart is that for some values, the test won't be able to return a correct result for all the 32bit numbers you want to test, for example, with divisibility by 7 :

    we got 0x100000000- (1/n)*0xFFFFFFFF = 0xDB6DB6DC and 7 * 0xDB6DB6DC = 0x6 0000 0004, We will only test one quarter of the values, but we can certainly avoid that with substractions.

    Other examples :

    11 * 0xE8BA2E8C = A0000 0004, one quarter of the values

    17 * 0xF0F0F0F1 = 10 0000 0000 1 comparing to 0xF0F0F0F Every values !

    Etc., we can even test every numbers by combining natural numbers between them.

    0 讨论(0)
  • 2020-12-01 02:00

    Given a number x. Convert x to a string. Parse the string character by character. Convert each parsed character to a number (using atoi()) and add up all these numbers into a new number y. Repeat the process until your final resultant number is one digit long. If that one digit is either 3,6 or 9, the origional number x is divisible by 3.

    0 讨论(0)
  • 2020-12-01 02:00

    Here is your optimized solution that every one should know.................

    Source: http://www.geeksforgeeks.org/archives/511

    #include<stdio.h>
    
    
    int isMultiple(int n)
    {
        int o_count = 0;
        int e_count = 0;
    
    
        if(n < 0)  
               n = -n;
        if(n == 0) 
               return 1;
        if(n == 1)
               return 0;
    
        while(n)
        {
    
            if(n & 1)
               o_count++;
            n = n>>1;
    
    
            if(n & 1)
                e_count++;
            n = n>>1;
        }
    
         return isMultiple(abs(o_count - e_count));
    }
    
    
    int main()
    {
        int num = 23;
        if (isMultiple(num))
            printf("multiple of 3");
        else
            printf(" not multiple of 3");
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-01 02:06

    My solution in Java only works for 32-bit unsigned ints.

    static boolean isDivisibleBy3(int n) {
      int x = n;
      x = (x >>> 16) + (x & 0xffff); // max 0x0001fffe
      x = (x >>> 8) + (x & 0x00ff); // max 0x02fd
      x = (x >>> 4) + (x & 0x000f); // max 0x003d (for 0x02ef)
      x = (x >>> 4) + (x & 0x000f); // max 0x0011 (for 0x002f)
      return ((011111111111 >> x) & 1) != 0;
    }
    

    It first reduces the number down to a number less than 32. The last step checks for divisibility by shifting the mask the appropriate number of times to the right.

    0 讨论(0)
  • 2020-12-01 02:07

    While the technique of converting to a string and then adding the decimal digits together is elegant, it either requires division or is inefficient in the conversion-to-a-string step. Is there a way to apply the idea directly to a binary number, without first converting to a string of decimal digits?

    It turns out, there is:

    Given a binary number, the sum of its odd bits minus the sum of its even bits is divisible by 3 iff the original number was divisible by 3.

    As an example: take the number 3726, which is divisible by 3. In binary, this is 111010001110. So we take the odd digits, starting from the right and moving left, which are [1, 1, 0, 1, 1, 1]; the sum of these is 5. The even bits are [0, 1, 0, 0, 0, 1]; the sum of these is 2. 5 - 2 = 3, from which we can conclude that the original number is divisible by 3.

    0 讨论(0)
  • 2020-12-01 02:09

    C# Solution for checking if a number is divisible by 3

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int num = 33;
                bool flag = false;
    
                while (true)
                {
                    num = num - 7;
                    if (num == 0)
                    {
                        flag = true;
                        break;
                    }
                    else if (num < 0)
                    {
                        break;
                    }
                    else
                    {
                        flag = false;
                    }
                }
    
                if (flag)
                    Console.WriteLine("Divisible by 3");
                else
                    Console.WriteLine("Not Divisible by 3");
    
                Console.ReadLine();
    
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题