The power of a number says how many times to use the number in a multiplication. Powers are also called Exponents or Indices. For example, 8^2 could be called “8 to the power 2” or “8 to the second power”, or simply “8 squared”.
Some interesting fact about Power :
- If the indices is 1, then you just have the number itself. For example, 5^1 = 5
- If the indices is 0, then you get 1. For example, 5^0 = 1
- Exponents make it easier to write and use many multiplications
- Negative exponent means how many times to divide one by the number.For example, 5^-1 = 1 /5 = 0.2
How we check if a number is power of y for a given integer x ?
Naive solution:
Given two positive numbers x and y, check if y is a power of x or not.
Examples :
Input: x = 10, y = 1
Output: True
Input: x = 10, y = 1000
Output: True
Input: x = 10, y = 1001
Output: False
Approach : A simple solution is to repeatedly compute powers of x. If a power becomes equal to y, then y is a power, else not.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPower( int x, long int y)
{
if (x == 1)
return (y == 1);
long int pow = 1;
while ( pow < y)
pow *= x;
return ( pow == y);
}
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
public class Test {
public static void main(String[] args)
{
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" );
}
public static boolean isPower( int x, int y)
{
if (x == 1 )
return (y == 1 );
int pow = 1 ;
while (pow < y)
pow = pow * x;
return (pow == y);
}
}
|
Python3
def isPower (x, y):
if (x = = 1 ):
return (y = = 1 )
pow = 1
while ( pow < y):
pow = pow * x
return ( pow = = y)
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#
using System;
class GFG
{
public static bool isPower ( int x, int y)
{
if (x == 1)
return (y == 1);
int pow = 1;
while (pow < y)
pow = pow * x;
return (pow == y);
}
public static void Main ()
{
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
<?php
function isPower( $x , $y )
{
if ( $x == 1)
return ( $y == 1 ? "True" : "False" );
$pow = 1;
while ( $pow < $y )
$pow *= $x ;
return ( $pow == $y ? "True" : "False" );
}
echo isPower(10, 1) . "\n" ;
echo isPower(1, 20) . "\n" ;
echo isPower(2, 128) . "\n" ;
echo isPower(2, 30) . "\n" ;
?>
|
Javascript
<script>
function isPower( x, y)
{
if (x == 1)
return (y == 1);
let pow = 1;
while (pow < y)
pow *= x;
return (pow == y);
}
document.write((isPower(10, 1) ? "True" : "False" ) + "</br>" );
document.write((isPower(1, 20) ? "True" : "False" ) + "</br>" );
document.write((isPower(2, 128) ? "True" : "False" ) + "</br>" );
document.write((isPower(2, 30) ? "True" : "False" ) + "</br>" );
</script>
|
Output:
True
False
True
False
Time complexity of above solution is O(Logxy)
Auxiliary Space: O(1)
Basic Program related to Power :
More problems related to Powers :
Recent Articles on Power!
Last Updated :
05 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...