📌  相关文章
📜  检查一个数 N 是否可以表示为 X 的幂的总和

📅  最后修改于: 2022-05-13 01:56:07.567000             🧑  作者: Mango

检查一个数 N 是否可以表示为 X 的幂的总和

给定两个正数NX ,任务是检查给定数N是否可以表示为X的不同幂的总和。如果发现为真,则打印“Yes” ,否则,打印“No”

例子:

方法:给定的问题可以通过检查数字N是否可以写在基数X中来解决。请按照以下步骤解决问题:

  • 迭代一个循环,直到N的值至少为 0并执行以下步骤:
    • N除以X时,计算余数rem的值。
    • 如果rem的值至少为 2 ,则打印“No”并返回。
    • 否则,将 N 的值更新为N / X
  • 完成上述步骤后,如果不存在任何终止,则打印“是” ,因为 N 处的结果可以用X的不同幂表示。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the number N
// can be expressed as the sum of
// different powers of X or not
bool ToCheckPowerofX(int n, int x)
{
    // While n is a positive number
    while (n > 0) {
 
        // Find the remainder
        int rem = n % x;
 
        // If rem is at least 2, then
        // representation is impossible
        if (rem >= 2) {
            return false;
        }
 
        // Divide the value of N by x
        n = n / x;
    }
 
    return true;
}
 
// Driver Code
int main()
{
    int N = 10, X = 3;
    if (ToCheckPowerofX(N, X)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to check if the number N
// can be expressed as the sum of
// different powers of X or not
static boolean ToCheckPowerofX(int n, int x)
{
     
    // While n is a positive number
    while (n > 0)
    {
         
        // Find the remainder
        int rem = n % x;
 
        // If rem is at least 2, then
        // representation is impossible
        if (rem >= 2)
        {
            return false;
        }
 
        // Divide the value of N by x
        n = n / x;
    }
    return true;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 10, X = 3;
    if (ToCheckPowerofX(N, X))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to check if the number N
# can be expressed as the sum of
# different powers of X or not
def ToCheckPowerofX(n, x):
     
    # While n is a positive number
    while (n > 0):
         
        # Find the remainder
        rem = n % x
 
        # If rem is at least 2, then
        # representation is impossible
        if (rem >= 2):
            return False
 
        # Divide the value of N by x
        n = n // x
 
    return True
 
# Driver Code
if __name__ == '__main__':
     
    N = 10
    X = 3
     
    if (ToCheckPowerofX(N, X)):
        print("Yes")
    else:
        print("No")
         
# This code is contributed by bgangwar59


C#
// C# program for the above approach
using System;
class GFG{
     
 
// Function to check if the number N
// can be expressed as the sum of
// different powers of X or not
static bool ToCheckPowerofX(int n, int x)
{
     
    // While n is a positive number
    while (n > 0)
    {
         
        // Find the remainder
        int rem = n % x;
 
        // If rem is at least 2, then
        // representation is impossible
        if (rem >= 2)
        {
            return false;
        }
 
        // Divide the value of N by x
        n = n / x;
    }
    return true;
}
 
// Driver code
public static void Main(String []args)
{
     int N = 10, X = 3;
    if (ToCheckPowerofX(N, X))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
// This code is contributed by code_hunt,


Javascript


输出:
Yes

时间复杂度: O(log N)
辅助空间: O(1)