📜  4的倍数(一种有趣的方法)

📅  最后修改于: 2021-04-24 21:59:05             🧑  作者: Mango

给定数字n,任务是检查此数字是否为4的倍数,而不使用+,-,*和/%运算符。
例子 :

Input: n = 4  Output - Yes
       n = 20 Output - Yes
       n = 19 Output - No

方法1(使用XOR)
对于n> 1的一个有趣的事实是,我们对1到n的所有数字进行XOR,如果结果等于n,则n是4的倍数,否则不是。

C++
// An interesting XOR based method to check if
// a number is multiple of 4.
#include
using namespace std;
 
// Returns true if n is a multiple of 4.
bool isMultipleOf4(int n)
{
    if (n == 1)
       return false;
 
    // Find XOR of all numbers from 1 to n
    int XOR = 0;
    for (int i = 1; i <= n; i++)
        XOR = XOR ^ i;
 
    // If XOR is equal n, then return true
    return (XOR == n);
}
 
// Driver code to print multiples of 4
int main()
{
    // Printing multiples of 4 using above method
    for (int n=0; n<=42; n++)
       if (isMultipleOf4(n))
         cout << n << " ";
    return 0;
}


Java
// An interesting XOR based method to check if
// a number is multiple of 4.
 
class Test
{
    // Returns true if n is a multiple of 4.
    static boolean isMultipleOf4(int n)
    {
        if (n == 1)
           return false;
      
        // Find XOR of all numbers from 1 to n
        int XOR = 0;
        for (int i = 1; i <= n; i++)
            XOR = XOR ^ i;
      
        // If XOR is equal n, then return true
        return (XOR == n);
    }
     
    // Driver method
    public static void main(String[] args)
    {
        // Printing multiples of 4 using above method
        for (int n=0; n<=42; n++)
           System.out.print(isMultipleOf4(n) ? n : " ");
    }
}


Python 3
# An interesting XOR based
# method to check if a
# number is multiple of 4.
 
# Returns true if n is a
# multiple of 4.
def isMultipleOf4(n):
 
    if (n == 1):
        return False
 
    # Find XOR of all numbers
    # from 1 to n
    XOR = 0
    for i in range(1, n + 1):
        XOR = XOR ^ i
 
    # If XOR is equal n, then
    # return true
    return (XOR == n)
 
# Driver code to print
# multiples of 4 Printing
# multiples of 4 using
# above method
for n in range(0, 43):
    if (isMultipleOf4(n)):
        print(n, end = " ")
 
# This code is contributed
# by Smitha


C#
// An interesting XOR based method
// to check if a number is multiple
// of 4.
using System;
class GFG {
     
    // Returns true if n is a
    // multiple of 4.
    static bool isMultipleOf4(int n)
    {
        if (n == 1)
        return false;
     
        // Find XOR of all numbers
        // from 1 to n
        int XOR = 0;
        for (int i = 1; i <= n; i++)
            XOR = XOR ^ i;
     
        // If XOR is equal n, then
        // return true
        return (XOR == n);
    }
     
    // Driver method
    public static void Main()
    {
         
        // Printing multiples of 4
        // using above method
        for (int n = 0; n <= 42; n++)
        {
            if (isMultipleOf4(n))
                Console.Write(n+" ");
        }
    }
}
 
// This code is contributed by Smitha.


PHP


Javascript


C++
// An interesting XOR based method to check if
// a number is multiple of 4.
#include
using namespace std;
 
// Returns true if n is a multiple of 4.
bool isMultipleOf4(long long n)
{
    if (n==0)
        return true;
 
    return (((n>>2)<<2) == n);
}
 
// Driver code to print multiples of 4
int main()
{
    // Printing multiples of 4 using above method
    for (int n=0; n<=42; n++)
        if (isMultipleOf4(n))
            cout << n << " ";
    return 0;
}


Java
// An interesting XOR based method to check if
// a number is multiple of 4.
 
class Test
{
    // Returns true if n is a multiple of 4.
    static boolean isMultipleOf4(long n)
    {
        if (n==0)
            return true;
      
        return (((n>>2)<<2) == n);
    }
     
    // Driver method
    public static void main(String[] args)
    {
        // Printing multiples of 4 using above method
        for (int n=0; n<=42; n++)
           System.out.print(isMultipleOf4(n) ? n : " ");
    }
}


C#
// An interesting XOR based method to
// check if a number is multiple of 4.
using System;
 
class GFG {
     
    // Returns true if n is a multiple
    // of 4.
    static bool isMultipleOf4(int n)
    {
        if (n == 0)
            return true;
     
        return (((n >> 2) << 2) == n);
    }
     
    // Driver code to print multiples
    // of 4
    static void Main()
    {
         
        // Printing multiples of 4 using
        // above method
        for (int n = 0; n <= 42; n++)
            if (isMultipleOf4(n))
                Console.Write(n + " ");
    }
}
 
// This code is contributed by Anuj_67


PHP
> 2) << 2) == $n);
}
 
// Driver Code
 
// Printing multiples of 4
// using above method
for ($n = 0; $n <= 42; $n++)
    if (isMultipleOf4($n))
        echo $n , " ";
         
// This code is contributed by anuj_67.
?>


输出 :

0 4 8 12 16 20 24 28 32 36 40 

这是如何运作的?
当我们对数字进行XOR运算时,在4的倍数之前得到0作为XOR值。这在4的倍数之前不断重复。

Number Binary-Repr  XOR-from-1-to-n
1         1           [0001]
2        10           [0011]
3        11           [0000]

方法2(使用按位移位运算符)
这个想法是使用>>删除最后两位,然后使用<<乘以4。如果最终结果与n相同,则最后两位为0,因此数字为4的倍数。

C++

// An interesting XOR based method to check if
// a number is multiple of 4.
#include
using namespace std;
 
// Returns true if n is a multiple of 4.
bool isMultipleOf4(long long n)
{
    if (n==0)
        return true;
 
    return (((n>>2)<<2) == n);
}
 
// Driver code to print multiples of 4
int main()
{
    // Printing multiples of 4 using above method
    for (int n=0; n<=42; n++)
        if (isMultipleOf4(n))
            cout << n << " ";
    return 0;
}

Java

// An interesting XOR based method to check if
// a number is multiple of 4.
 
class Test
{
    // Returns true if n is a multiple of 4.
    static boolean isMultipleOf4(long n)
    {
        if (n==0)
            return true;
      
        return (((n>>2)<<2) == n);
    }
     
    // Driver method
    public static void main(String[] args)
    {
        // Printing multiples of 4 using above method
        for (int n=0; n<=42; n++)
           System.out.print(isMultipleOf4(n) ? n : " ");
    }
}

C#

// An interesting XOR based method to
// check if a number is multiple of 4.
using System;
 
class GFG {
     
    // Returns true if n is a multiple
    // of 4.
    static bool isMultipleOf4(int n)
    {
        if (n == 0)
            return true;
     
        return (((n >> 2) << 2) == n);
    }
     
    // Driver code to print multiples
    // of 4
    static void Main()
    {
         
        // Printing multiples of 4 using
        // above method
        for (int n = 0; n <= 42; n++)
            if (isMultipleOf4(n))
                Console.Write(n + " ");
    }
}
 
// This code is contributed by Anuj_67

的PHP

> 2) << 2) == $n);
}
 
// Driver Code
 
// Printing multiples of 4
// using above method
for ($n = 0; $n <= 42; $n++)
    if (isMultipleOf4($n))
        echo $n , " ";
         
// This code is contributed by anuj_67.
?>

输出 :

0 4 8 12 16 20 24 28 32 36 40