📌  相关文章
📜  检查数字是否可以表示为x ^ y(x升为幂y)

📅  最后修改于: 2021-04-29 10:01:20             🧑  作者: Mango

给定一个正整数n,查找它是否可以表示为x y ,其中y> 1和x>0。x和y都是整数。

例子 :

Input:  n = 8
Output: true
8 can be expressed as 23

Input:  n = 49
Output: true
49 can be expressed as 72

Input:  n = 48
Output: false
48 can't be expressed as xy

尝试从2到n的平方根(给定数)的所有数字x的想法很简单。对于每个x,请尝试x ^ y,其中y从2开始并加一,直到x ^ y变为n或大于n。

以下是上述想法的实现。

C++
// C++ program to check if a given number can be expressed
// as power
#include 
using namespace std;
 
// Returns true if n can be written as x^y
bool isPower(unsigned n)
{
    if (n==1)  return true;
 
    // Try all numbers from 2 to sqrt(n) as base
    for (int x=2; x<=sqrt(n); x++)
    {
        unsigned y = 2;
        unsigned p = pow(x, y);
 
        // Keep increasing y while power 'p' is smaller
        // than n.
        while (p<=n && p>0)
        {
            if (p==n)
                return true;
            y++;
            p = pow(x, y);
         }
    }
    return false;
}
 
// Driver Program
int main()
{
    for (int i =2; i<100; i++)
        if (isPower(i))
           cout << i << "  ";
    return 0;
}


Java
// Java code to check if a number can be expressed
// as x^y (x raised to power y)
class GFG {
 
    // Returns true if n can be written as x^y
    static boolean isPower(int n)
    {
        for (int x = 2; x <= Math.sqrt(n); x++) {
            int y = 2;
 
            double p = Math.pow(x, y);
 
            while (p <= n && p > 0) {
                if (p == n)
                    return true;
                y++;
                p = Math.pow(x, y);
            }
        }
        return false;
    }
 
    // Driver function
    public static void main(String[] args)
    {
        for (int i = 2; i < 100; i++)
            if (isPower(i))
                System.out.print(i + " ");
    }
}
 
// This code is submitted by Kamal Rawal


Python3
# Python3 program to check if
# a given number can be expressed
# as power
import math
 
# Returns true if n can be written as x^y
def isPower(n) :
    if (n==1)  :
        return True
  
    # Try all numbers from 2 to sqrt(n) as base
    for x in range(2,(int)(math.sqrt(n))+1) :
        y = 2
        p = (int)(math.pow(x, y))
  
        # Keep increasing y while power 'p' is smaller
        # than n.
        while (p<=n and p>0) :
            if (p==n) :
                return True
             
            y = y + 1
            p = math.pow(x, y)
          
          
    return False
     
  
# Driver Program
for i in range(2,100 ) :
    if (isPower(i)) :
        print(i,end=" ")
         
         
# This code is contributed by Nikita Tiwari.


C#
// C# code to check if a number
// can be expressed as x^y
// (x raised to power y)
using System;
 
class GFG
{
    // Returns true if n can
    // be written as x^y
    static bool isPower(int n)
    {
        for (int x = 2; x <= Math.Sqrt(n); x++)
        {
            int y = 2;
 
            double p = Math.Pow(x, y);
 
            while (p <= n && p > 0)
            {
                if (p == n)
                    return true;
                y++;
                p = Math.Pow(x, y);
            }
        }
        return false;
    }
 
    // Driver Code
    static public void Main ()
    {
        for (int i = 2; i < 100; i++)
            if (isPower(i))
                Console.Write(i + " ");
    }
}
 
// This code is submitted by ajit.


PHP
 0)
        {
            if ($p == $n)
                return true;
            $y++;
            $p = pow($x, $y);
        }
    }
    return false;
}
 
// Driver Code
for ($i = 2; $i < 100; $i++)
    if (isPower($i))
    echo $i , " ";
 
// This code is contributed by aj_36
?>


Javascript


C++
// C++ program to check if a given number can be expressed
// as power
#include 
using namespace std;
 
// Returns true if n can be written as x^y
bool isPower(unsigned int n)
{
    // Base case
    if (n <= 1) return true;
 
    // Try all numbers from 2 to sqrt(n) as base
    for (int x=2; x<=sqrt(n); x++)
    {
        unsigned  p = x;
 
        // Keep multiplying p with x while is smaller
        // than or equal to x
        while (p <= n)
        {
            p *= x;
            if (p == n)
                return true;
        }
    }
    return false;
}
 
// Driver Program
int main()
{
    for (int i =2; i<100; i++)
        if (isPower(i))
           cout << i << "  ";
    return 0;
}


Java
// Java code to check if a number can be expressed
// as x^y (x raised to power y)
class GFG {
     
    // Returns true if n can be written as x^y
    static boolean isPower(int n)
    {
        for (int x = 2; x <= Math.sqrt(n); x++) {
            int p = x;
 
            while (p <= n) {
                p = p * x;
                if (p == n)
                    return true;
            }
        }
        return false;
    }
 
    // Driver function
    public static void main(String[] args)
    {
        for (int i = 2; i < 100; i++)
            if (isPower(i))
                System.out.print(i + " ");
    }
}
 
// This code is submitted by Kamal Rawal


Python3
# Python3 program to check if
# a given number can be expressed
# as power
import math
 
# Returns true if n can be written
# as x ^ y
def isPower(n) :
 
    # Base case
    if (n <= 1) :
        return True
 
    # Try all numbers from 2 to sqrt(n)
    # as base
    for x in range(2, (int)(math.sqrt(n)) + 1) :
        p = x
 
        # Keep multiplying p with x while
        # is smaller than or equal to x
        while (p <= n) :
            p = p * x
             
            if (p == n) :
                return True
         
    return False
     
# Driver Program
for i in range(2, 100) :
     
    if (isPower(i)) :
        print( i, end =" ")
         
# This code is contributed by Nikita Tiwari.


C#
// C# code to check if a number
// can be expressed as x^y (x
// raised to power y)
using System;
 
class GFG
{
     
    // Returns true if n can
    // be written as x^y
    static bool isPower(int n)
    {
        for (int x = 2;
                 x <= Math.Sqrt(n); x++)
        {
            int p = x;
 
            while (p <= n)
            {
                p = p * x;
                if (p == n)
                    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 submitted by nitin mittal.


PHP


Javascript


C++
// C++ program to check if a given number can be expressed
// as power
#include 
using namespace std;
 
// Returns true if n can be written as x^y
bool isPower(unsigned n)
{
    float p;
    if (n <= 1)
        return 1;
    for (int i = 2; i <= sqrt(n); i++) {
        p = log2(n) / log2(i);
        if ((ceil(p) == floor(p)) && p > 1)
            return true;
    }
    return false;
}
 
// Driver Program
int main()
{
    for (int i = 2; i < 100; i++)
        if (isPower(i))
            cout << i << " ";
    return 0;
}


Java
// Java program to check if a given
// number can be expressed as power
import java.io.*;
import java.lang.Math;
 
class GFG {
     
// Returns true if n can be written as x^y
static boolean isPower(int n)
{
    double p;
    if (n <= 1)
    {
        return true;
    }
    for(int i = 2; i <= Math.sqrt(n); i++)
    {
       p = Math.log(n) / Math.log(i);
 
       if ((Math.ceil(p) == Math.floor(p)) && p > 1)
       {
           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 shubhamsingh10


Python3
# Python3 program to check if a given
# number can be expressed as power
import math
 
# Returns true if n can be
# written as x^y
def isPower(n):
     
    p = 0
     
    if (n <= 1):
        return 1
         
    for i in range(2, (int)(math.sqrt(n)) + 1):
        p = math.log2(n) / math.log2(i)
         
        if ((math.ceil(p) ==
            math.floor(p)) and p > 1):
            return 1
             
    return 0
     
# Driver code
for i in range(2, 100):
    if isPower(i):
        print(i, end = " ")
 
# This code is contributed by sallagondaavinashreddy7


C#
// C# program to check if a given
// number can be expressed as power
using System;
 
class GFG{
     
// Returns true if n can be written as x^y
static bool isPower(int n)
{
    double p;
    if (n <= 1)
    {
        return true;
    }
     
    for(int i = 2; i <= Math.Sqrt(n); i++)
    {
       p = Math.Log(n) / Math.Log(i);
        
       if ((Math.Ceiling(p) == Math.Floor(p)) && p > 1)
       {
           return true;
       }
    }
    return false;
}
     
// Driver code
static public void Main ()
{
    for(int i = 2; i < 100; i++)
    {
       if (isPower(i))
           Console.Write(i + " ");
    }
}
}
 
// This code is contributed by shubhamsingh10


Javascript


输出 :

4 8 9 16 25 27 32 36 49 64 81 

上述解决方案中的一种优化是通过将p与x乘以1来避免调用pow()。

C++

// C++ program to check if a given number can be expressed
// as power
#include 
using namespace std;
 
// Returns true if n can be written as x^y
bool isPower(unsigned int n)
{
    // Base case
    if (n <= 1) return true;
 
    // Try all numbers from 2 to sqrt(n) as base
    for (int x=2; x<=sqrt(n); x++)
    {
        unsigned  p = x;
 
        // Keep multiplying p with x while is smaller
        // than or equal to x
        while (p <= n)
        {
            p *= x;
            if (p == n)
                return true;
        }
    }
    return false;
}
 
// Driver Program
int main()
{
    for (int i =2; i<100; i++)
        if (isPower(i))
           cout << i << "  ";
    return 0;
}

Java

// Java code to check if a number can be expressed
// as x^y (x raised to power y)
class GFG {
     
    // Returns true if n can be written as x^y
    static boolean isPower(int n)
    {
        for (int x = 2; x <= Math.sqrt(n); x++) {
            int p = x;
 
            while (p <= n) {
                p = p * x;
                if (p == n)
                    return true;
            }
        }
        return false;
    }
 
    // Driver function
    public static void main(String[] args)
    {
        for (int i = 2; i < 100; i++)
            if (isPower(i))
                System.out.print(i + " ");
    }
}
 
// This code is submitted by Kamal Rawal

Python3

# Python3 program to check if
# a given number can be expressed
# as power
import math
 
# Returns true if n can be written
# as x ^ y
def isPower(n) :
 
    # Base case
    if (n <= 1) :
        return True
 
    # Try all numbers from 2 to sqrt(n)
    # as base
    for x in range(2, (int)(math.sqrt(n)) + 1) :
        p = x
 
        # Keep multiplying p with x while
        # is smaller than or equal to x
        while (p <= n) :
            p = p * x
             
            if (p == n) :
                return True
         
    return False
     
# Driver Program
for i in range(2, 100) :
     
    if (isPower(i)) :
        print( i, end =" ")
         
# This code is contributed by Nikita Tiwari.

C#

// C# code to check if a number
// can be expressed as x^y (x
// raised to power y)
using System;
 
class GFG
{
     
    // Returns true if n can
    // be written as x^y
    static bool isPower(int n)
    {
        for (int x = 2;
                 x <= Math.Sqrt(n); x++)
        {
            int p = x;
 
            while (p <= n)
            {
                p = p * x;
                if (p == n)
                    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 submitted by nitin mittal.

的PHP


Java脚本


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

替代实现:

C++

// C++ program to check if a given number can be expressed
// as power
#include 
using namespace std;
 
// Returns true if n can be written as x^y
bool isPower(unsigned n)
{
    float p;
    if (n <= 1)
        return 1;
    for (int i = 2; i <= sqrt(n); i++) {
        p = log2(n) / log2(i);
        if ((ceil(p) == floor(p)) && p > 1)
            return true;
    }
    return false;
}
 
// Driver Program
int main()
{
    for (int i = 2; i < 100; i++)
        if (isPower(i))
            cout << i << " ";
    return 0;
}

Java

// Java program to check if a given
// number can be expressed as power
import java.io.*;
import java.lang.Math;
 
class GFG {
     
// Returns true if n can be written as x^y
static boolean isPower(int n)
{
    double p;
    if (n <= 1)
    {
        return true;
    }
    for(int i = 2; i <= Math.sqrt(n); i++)
    {
       p = Math.log(n) / Math.log(i);
 
       if ((Math.ceil(p) == Math.floor(p)) && p > 1)
       {
           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 shubhamsingh10

Python3

# Python3 program to check if a given
# number can be expressed as power
import math
 
# Returns true if n can be
# written as x^y
def isPower(n):
     
    p = 0
     
    if (n <= 1):
        return 1
         
    for i in range(2, (int)(math.sqrt(n)) + 1):
        p = math.log2(n) / math.log2(i)
         
        if ((math.ceil(p) ==
            math.floor(p)) and p > 1):
            return 1
             
    return 0
     
# Driver code
for i in range(2, 100):
    if isPower(i):
        print(i, end = " ")
 
# This code is contributed by sallagondaavinashreddy7

C#

// C# program to check if a given
// number can be expressed as power
using System;
 
class GFG{
     
// Returns true if n can be written as x^y
static bool isPower(int n)
{
    double p;
    if (n <= 1)
    {
        return true;
    }
     
    for(int i = 2; i <= Math.Sqrt(n); i++)
    {
       p = Math.Log(n) / Math.Log(i);
        
       if ((Math.Ceiling(p) == Math.Floor(p)) && p > 1)
       {
           return true;
       }
    }
    return false;
}
     
// Driver code
static public void Main ()
{
    for(int i = 2; i < 100; i++)
    {
       if (isPower(i))
           Console.Write(i + " ");
    }
}
}
 
// This code is contributed by shubhamsingh10

Java脚本


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