📌  相关文章
📜  检查数字是否可以表示为2 ^ x + 2 ^ y

📅  最后修改于: 2021-05-25 09:11:22             🧑  作者: Mango

给定一个数字n,我们需要检查它是否可以表示为2 x + 2 y 。在这里x和y可以相等。
例子 :

Input  : 24
Output : Yes 
Explanation: 24 can be expressed as  
24 + 23 

Input  : 13
output : No
Explanation: It is not possible to 
express 13 as sum of two powers of 2.

如果我们举几个例子,我们会注意到,如果一个数字已经是2的幂(对于n> 1)或减去之前的幂后得到的余数,则数字可以2 ^ x + 2 ^ y的形式表示的2的乘方也是2的幂。
以下是上述想法的实现

C++
// CPP code to check if a number can be
// expressed as  2^x + 2^y
#include 
using namespace std;
 
// Utility function to check if
// a number is power of 2 or not
bool isPowerOfTwo(int n)
{
    return (n && !(n & (n - 1)));
}
 
// Utility function to determine the
// value of previous power of 2
int previousPowerOfTwo(int n)
{
    while (n & n - 1) {
        n = n & n - 1;
    }
    return n;
}
 
// function to check if n can be expressed
// as 2^x + 2^y or not
bool checkSum(int n)
{
    // if value of n is 0 or 1
    // it can not be expressed as
    // 2^x + 2^y
    if (n == 0 || n == 1)
       return false;
 
    // if a number is power of 2
    // then it can be expressed as
    // 2^x + 2^y
    else if (isPowerOfTwo(n)) {
        cout << " " << n / 2 << " " << n / 2;
        return true;
    }
 
    else {
        // if the remainder after
        // subtracting previous power of 2
        // is also a power of 2 then
        // it can be expressed as
        // 2^x + 2^y
        int x = previousPowerOfTwo(n);
        int y = n - x;
        if (isPowerOfTwo(y)) {
            cout << " " << x << " " << y;
            return true;
        }
    }
 
    return false;
}
 
// driver code
int main()
{
    int n1 = 20;
    if (checkSum(n1) == false)
        cout << "No";
 
    cout << endl;
    int n2 = 11;
    if (checkSum(n2) == false)
        cout << "No";
 
    return 0;
}


Java
// Java code to check if a number
// can be expressed as 2^x + 2^y
 
class GFG {
 
    // Utility function to check if
    // a number is power of 2 or not
    static boolean isPowerOfTwo(int n)
    {
        return n != 0 && ((n & (n - 1)) == 0);
    }
 
    // Utility function to determine the
    // value of previous power of 2
    static int previousPowerOfTwo(int n)
    {
        while ((n & n - 1) > 1) {
            n = n & n - 1;
        }
        return n;
    }
 
    // function to check if
    // n can be expressed as
    // 2^x + 2^y or not
    static boolean checkSum(int n)
    {
        // if value of n is 0 or 1
        // it can not be expressed as
        // 2^x + 2^y
        if (n == 0 || n == 1)
            return false;
 
        // if a number is power of 2
        // it can be expressed as
        // 2^x + 2^y
 
        else if (isPowerOfTwo(n)) {
            System.out.println(n / 2 + " " + n / 2);
        }
        else {
 
            // if the remainder after
            // subtracting previous power of 2
            // is also a power of 2 then
            // it can be expressed as
            // 2^x + 2^y
            int x = previousPowerOfTwo(n);
            int y = n - x;
            if (isPowerOfTwo(y)) {
 
                System.out.println(x + " " + y);
                return true;
            }
        }
 
         return false;
    }
    // driver code
    public static void main(String[] argc)
    {
    int n1 = 20;
    if (checkSum(n1) == false)
        System.out.println("No");
 
    System.out.println();
    int n2 = 11;
    if (checkSum(n2) == false)
        System.out.println("No");
    }
}


Python3
# Python3 code to check if a number
# can be expressed as
# 2 ^ x + 2 ^ y
 
# Utility function to check if
# a number is power of 2 or not
def isPowerOfTwo( n):
    return (n and (not(n & (n - 1))) )
             
     
# Utility function to determine the
# value of previous power of 2       
def previousPowerOfTwo( n ):   
    while( n & n-1 ):
        n = n & n - 1
    return n
     
# function to check if
# n can be expressed as
# 2 ^ x + 2 ^ y or not
def checkSum(n):
 
        # if value of n is 0 or 1
        # it can not be expressed as
        # 2 ^ x + 2 ^ y
        if (n == 0 or n == 1 ):
            return False
             
        # if n is power of two then
        # it can be expressed as
        # sum of 2 ^ x + 2 ^ y
        elif(isPowerOfTwo(n)):
            print(n//2, n//2)
            return True
             
        # if the remainder after
        # subtracting previous power of 2
        # is also a power of 2 then
        # it can be expressed as
        # 2 ^ x + 2 ^ y
        else:
                x = previousPowerOfTwo(n)
                y = n-x;
                if (isPowerOfTwo(y)):
                    print(x, y)
                    return True
                else:                   
                    return False
                     
# driver code
n1 = 20
if (checkSum(n1)):
  print("No")
 
n2 = 11
if (checkSum(n2)):
  print("No")


C#
// C# code to check if a number
// can be expressed as
// 2^x + 2^y
 
using System;
class GFG {
 
    // Utility function to check if
    // a number is power of 2 or not
    static bool isPowerOfTwo(int n)
    {
        return n != 0 && ((n & (n - 1)) == 0);
    }
 
    // Utility function to determine the
    // value of previous power of 2
    static int previousPowerOfTwo(int n)
    {
 
        while ((n & n - 1) > 1) {
            n = n & n - 1;
        }
        return n;
    }
 
    // function to check if
    // n can be expressed as
    // 2^x + 2^y or not
    static bool checkSum(int n)
    {
        // if value of n is 0 or 1
        // it can not be expressed as
        // 2^x + 2^y
        if (n == 0 || n == 1) {
            Console.WriteLine("No");
            return false;
        }
 
        // if a number is power of
        // it can be expressed as
        // 2^x + 2^y
 
        else if (isPowerOfTwo(n)) {
            Console.WriteLine(n / 2 + " " + n / 2);
            return true;
        }
 
        else {
 
            // if the remainder after
            // subtracting previous power  of 2
            // is also a power of 2 then
            // it can be expressed as
            // 2^x + 2^y
 
            int x = previousPowerOfTwo(n);
            int y = n - x;
            if (isPowerOfTwo(y)) {
                Console.WriteLine(x + " " + y);
                return true;
            }
            else {
                return false;
            }
        }
    }
    // driver code
    public static void Main()
    {
    int n1 = 20;
    if (checkSum(n1) == false)
        Console.WriteLine("No");
 
    Console.WriteLine();
    int n2 = 11;
    if (checkSum(n2) == false)
        Console.WriteLine("No");
    }
}


PHP


Javascript


输出:
16 4
No