数字的幂表示乘法中使用数字的次数。幂也称为指数或指数。例如,8 ^ 2可以称为“ 8的幂2”或“ 8的第二幂”,或简称为“ 8平方”。
关于Power的一些有趣的事实:
- 如果索引是1,那么您只需要数字本身。例如5 ^ 1 = 5
- 如果索引为0,则得到1。例如5 ^ 0 = 1
- 指数使编写和使用许多乘法变得更容易
- 负指数表示将某数除以多少次,例如5 ^ -1 = 1/5 = 0.2
对于给定的整数x,我们如何检查数字是否为y的幂?
天真的解决方案:
给定两个正数x和y,请检查y是否为x的幂。
例子 :
Input: x = 10, y = 1
Output: True
Input: x = 10, y = 1000
Output: True
Input: x = 10, y = 1001
Output: False
方法:一个简单的解决方案是重复计算x的幂。如果幂等于y,则y为幂,否则为非。
C++
// C++ program to check if a number is power of
// another number
#include
using namespace std;
/* Returns 1 if y is a power of x */
bool isPower(int x, long int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly comput power of x
long int pow = 1;
while (pow < y)
pow *= x;
// Check if power of x becomes y
return (pow == y);
}
/* Driver program to test above function */
int main()
{
cout << (isPower(10, 1) ? "True" : "False") << endl;
cout << (isPower(1, 20) ? "True" : "False") << endl;
cout << (isPower(2, 128) ? "True" : "False") << endl;
cout << (isPower(2, 30) ? "True" : "False") << endl;
return 0;
}
Java
// Java program to check if a number is power of
// another number
public class Test {
// driver method to test power method
public static void main(String[] args)
{
// check the result for true/false and print.
System.out.println(isPower(10, 1) ? "True" : "False");
System.out.println(isPower(1, 20) ? "True" : "False");
System.out.println(isPower(2, 128) ? "True" : "False");
System.out.println(isPower(2, 30) ? "True" : "False");
}
/* Returns true if y is a power of x */
public static boolean isPower(int x, int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
int pow = 1;
while (pow < y)
pow = pow * x;
// Check if power of x becomes y
return (pow == y);
}
}
Python3
# Python program to check
# if a number is power of
# another number
# Returns true if y is a
# power of x
def isPower (x, y):
# The only power of 1
# is 1 itself
if (x == 1):
return (y == 1)
# Repeatedly compute
# power of x
pow = 1
while (pow < y):
pow = pow * x
# Check if power of x
# becomes y
return (pow == y)
# Driver Code
# check the result for
# true/false and print.
if(isPower(10, 1)): print("True")
else: print("False")
if(isPower(1, 20)): print("True")
else: print("False")
if(isPower(2, 128)): print("True")
else: print("False")
if(isPower(2, 30)): print("True")
else: print("False")
C#
// C# program to check if a number
// is power of another number
using System;
class GFG
{
// Returns true if y is a power of x
public static bool isPower (int x, int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
int pow = 1;
while (pow < y)
pow = pow * x;
// Check if power of x becomes y
return (pow == y);
}
// Driver Code
public static void Main ()
{
//check the result for true/false and print.
Console.WriteLine(isPower(10, 1) ? "True" : "False");
Console.WriteLine(isPower(1, 20) ? "True" : "False");
Console.WriteLine(isPower(2, 128) ? "True" : "False");
Console.WriteLine(isPower(2, 30) ? "True" : "False");
}
}
PHP
输出:
True
False
True
False
上述解决方案的时间复杂度为O(Log x y)
与电源有关的基本程序:
- 查找升为y的x的单位数字
- 检查数字是否可以表示为x ^ y(x升为幂y)
- 找到最接近a ^ b的x的倍数
- N位的所有可能数字和基数B(不带前导零)
- 检查数字是否可以表示为a ^ b |套装2
- 无需使用乘法(*)和除法(/)运算符即可编写自己的Power
与权力有关的更多问题:
- 元素乘积为偶数的子集总数
- 前N个自然数之和,不是K的幂
- 一个数字的GCD升为一个幂而另一个数字
- 最大N位数字可被给定的三个数字整除
- 检查给定的号码是否被电源隔离
最近关于权力的文章!