📌  相关文章
📜  检查数字是否可以表示为幂|设置2(使用日志)

📅  最后修改于: 2021-04-27 23:47:28             🧑  作者: Mango

检查数字是否可以表示为x ^ y(x升为幂y)
给定一个正整数n,查找它是否可以表示为x ^ y,其中y> 1和x>0。x和y都是整数。
例子 :

Input:  n = 8
Output: true
8 can be expressed as 2^3

Input:  n = 49
Output: true
49 can be expressed as 7^2

Input:  n = 48
Output: false
48 can't be expressed as x^y

我们在下面的文章中讨论了两种不同的方法。
检查数字是否可以表示为x ^ y(x升为幂y)。
这个想法是找到从2到n的平方根的不同底数的Log n。如果基数的Log n变为整数,则结果为true,否则结果为false。

C++
// CPP program to find if a number
// can be expressed as x raised to
// power y.
#include 
using namespace std;
 
bool isPower(unsigned int n)
{
    // Find Log n in different bases
    // and check if the value is an
    // integer
    for (int x=2; x<=sqrt(n); x++) {
        float f = log(n) / log(x);
        if ((f - (int)f) == 0.0)
            return true;       
    }
    return false;
}
 
// Driver code
int main()
{
    for (int i = 2; i < 100; i++)
        if (isPower(i))
            cout << i << "  ";
    return 0;
}


Java
// Java program to remove all the
// characters other then alphabets
class GFG {
     
    static boolean isPower(int n)
    {
        // Find Log n in different
        // bases and check if the
        // value is an integer
        for (int x = 2; x <=
               (int)Math.sqrt(n); x++)
        {
            float f = (float)Math.log(n) /
                      (float) Math.log(x);
                       
            if ((f - (int)f) == 0.0)
                return true;    
        }
        return false;
    }
     
    // Driver code
    public static void main(String args[])
    {
        for (int i = 2; i < 100; i++)
            if (isPower(i))
                System.out.print( i + " ");
    }
}
 
// This code is contributed by Sam007


Python3
# Python3 program to find if a number
# can be expressed as x raised to
# power y.
import math
 
def isPower(n):
 
    # Find Log n in different
    # bases and check if the
    # value is an integer
    for x in range(2,int(math.sqrt(n)) + 1):
     
        f = math.log(n) / math.log(x);
        if ((f - int(f)) == 0.0):
            return True;    
     
    return False;
 
# Driver code
for i in range(2, 100):
    if (isPower(i)):
        print(i, end = " ");
 
# This code is contributed by mits


C#
// C# program to remove all the
// characters other then alphabets
using System;
 
class GFG
{
    static bool isPower(int n)
    {
        // Find Log n in different
        // bases and check if the
        // value is an integer
        for (int x = 2;
                 x <= (int)Math.Sqrt(n); x++)
        {
            float f = (float)Math.Log(n) /
                      (float) Math.Log(x);
            if ((f - (int)f) == 0.0)
                return true;    
        }
        return false;
    }
    // Driver Code
    public static void Main()
    {
        for (int i = 2; i < 100; i++)
            if (isPower(i))
                Console.Write( i + " ");
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出:
4  8  9  16  25  27  32  36  49  64  81