📜  编写一种有效的方法来检查数字是否为3的倍数

📅  最后修改于: 2021-04-26 06:02:26             🧑  作者: Mango

我们想到的第一个解决方案是我们在学校中学到的解决方案。如果数字中的数字总和是3的倍数,则数字是3的倍数,例如,对于612,数字的总和是9,因此它是3的倍数。但是这种解决方案效率不高。您必须一一获取所有十进制数字,将它们相加,然后检查sum是否为3的倍数。

数字的二进制表示形式中有一个模式,可用于确定数字是否为3的倍数。如果奇数设置位(在奇数位置设置的位)与偶数设置位的计数之间的差为3的倍数,则为数字。
范例: 23(00..10111)
1)获取奇数位置所有置位的计数(对于23,为3)。
2)获取偶数位置的所有设置位的计数(对于23,则为1)。
3)如果上述两个计数的差是3的倍数,则数字也是3的倍数。
(对于23,它是2,所以23不是3的倍数)
再举一些例子,例如21、15等…

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)

证明:
通过以十进制数字11为例可以证明以上内容。 (在这种情况下,十进制数字中的11与二进制数字中的3相同)
如果奇数和偶数之和之间的差是11的倍数,那么十进制数是11的倍数。让我们看看如何。
让我们以十进制2位数字为例
AB = 11A -A + B = 11A +(B – A)
因此,如果(B – A)是11的倍数,则AB是。
让我们以3位数字表示。
ABC = 99A + A + 11B – B + C =(99A + 11B)+(A + C – B)
因此,如果(A + C – B)为11的倍数,则为(ABC)
现在让我们取4位数字。
ABCD = 1001A + D + 11C – C + 999B + B – A
=(1001A – 999B + 11C)+(D + B – A -C)
因此,如果(B + D – A – C)是11的倍数,则ABCD为ABCD。
可以对所有十进制数字继续执行此操作。
上面的概念可以用相同的方式证明为3的二进制数。
时间复杂度: O(logn)
程序:

C++
// CPP program to check if n is a multiple of 3
#include 
using namespace std;
 
/* Function to check if n is a multiple of 3*/
int isMultipleOf3(int n)
{
    int odd_count = 0;
    int even_count = 0;
 
    /* Make no positive if +n is multiple of 3
    then is -n. We are doing this to avoid
    stack overflow in recursion*/
    if (n < 0)
        n = -n;
    if (n == 0)
        return 1;
    if (n == 1)
        return 0;
 
    while (n) {
        /* If odd bit is set then
        increment odd counter */
        if (n & 1)
            odd_count++;
 
        /* If even bit is set then
        increment even counter */
        if (n & 2)
            even_count++;
        n = n >> 2;
    }
 
    return isMultipleOf3(abs(odd_count - even_count));
}
 
/* Program to test function isMultipleOf3 */
int main()
{
    int num = 24;
    if (isMultipleOf3(num))
        printf("%d is multiple of 3", num);
    else
        printf("%d is not a multiple of 3", num);
    return 0;
}


Java
// Java program to check if
// n is a multiple of 3
import java.lang.*;
import java.util.*;
 
class GFG {
    // Function to check if n
    // is a multiple of 3
    static int isMultipleOf3(int n)
    {
        int odd_count = 0;
        int even_count = 0;
 
        /* Make no positive if +n is multiple
    of 3 then is -n. We are doing this to
    avoid stack overflow in recursion*/
        if (n < 0)
            n = -n;
        if (n == 0)
            return 1;
        if (n == 1)
            return 0;
 
        while (n != 0) {
            /* If odd bit is set then
        increment odd counter */
            if ((n & 1) != 0)
                odd_count++;
 
            /* If even bit is set then
        increment even counter */
            if ((n & 2) != 0)
                even_count++;
 
            n = n >> 2;
        }
 
        return isMultipleOf3(Math.abs(odd_count - even_count));
    }
 
    /* Program to test function isMultipleOf3 */
    public static void main(String[] args)
    {
        int num = 24;
 
        if (isMultipleOf3(num) != 0)
            System.out.println(num + " is multiple of 3");
        else
            System.out.println(num + " is not a multiple of 3");
    }
}
 
// This code is contributed by Sahil_Bansall


Python3
# Python profram to check if n is a multiple of 3
 
# Function to check if n is a multiple of 3
def isMultipleOf3(n):
 
    odd_count = 0
    even_count = 0
 
    # Make no positive if + n is multiple of 3
    # then is -n. We are doing this to avoid
    # stack overflow in recursion
    if(n < 0):
        n = -n
    if(n == 0):
        return 1
    if(n == 1):
        return 0
 
    while(n):
         
        # If odd bit is set then
        # increment odd counter
        if(n & 1):
            odd_count += 1
 
        # If even bit is set then
        # increment even counter
        if(n & 2):
            even_count += 1
        n = n >> 2
 
    return isMultipleOf3(abs(odd_count - even_count))
 
# Program to test function isMultipleOf3
num = 24
if (isMultipleOf3(num)):
    print(num, 'is multiple of 3')
else:
    print(num, 'is not a multiple of 3')
 
# This code is contributed by Danish Raza


C#
// C# program to check if
// n is a multiple of 3
using System;
 
class GFG {
 
    // Function to check if n
    // is a multiple of 3
    static int isMultipleOf3(int n)
    {
        int odd_count = 0, even_count = 0;
 
        /* Make no positive if +n is multiple
        of 3 then is -n. We are doing this to
        avoid stack overflow in recursion*/
        if (n < 0)
            n = -n;
        if (n == 0)
            return 1;
        if (n == 1)
            return 0;
 
        while (n != 0) {
 
            /* If odd bit is set then
            increment odd counter */
            if ((n & 1) != 0)
                odd_count++;
 
            /* If even bit is set then
            increment even counter */
            if ((n & 2) != 0)
                even_count++;
 
            n = n >> 2;
        }
 
        return isMultipleOf3(Math.Abs(odd_count - even_count));
    }
 
    // Driver Code
    public static void Main()
    {
        int num = 24;
 
        if (isMultipleOf3(num) != 0)
            Console.Write(num + " is multiple of 3");
        else
            Console.Write(num + " is not a multiple of 3");
    }
}
 
// This code is contributed by Sam007


PHP
> 2;
    }
 
    return isMultipleOf3(abs($odd_count -
                             $even_count));
}
 
// Driver Code
$num = 24;
if (isMultipleOf3($num))
    echo $num, "is multiple of 3";
else
    echo $num, "is not a multiple of 3";
     
// This code is contributed by vt_m.
?>


Javascript


输出 :

24 is multiple of 3