Given a positive integer n, find if it can be expressed as xy where y > 1 and x > 0. x and y both are integers.
Examples :
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
The idea is simple to try all numbers x starting from 2 to square root of n (given number). For every x, try x^y where y starts from 2 and increases one by one until either x^y becomes n or greater than n.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPower(unsigned n)
{
if (n==1) return true ;
for ( int x=2; x<= sqrt (n); x++)
{
unsigned y = 2;
unsigned p = pow (x, y);
while (p<=n && p>0)
{
if (p==n)
return true ;
y++;
p = pow (x, y);
}
}
return false ;
}
int main()
{
for ( int i =2; i<100; i++)
if (isPower(i))
cout << i << " " ;
return 0;
}
|
Java
class GFG {
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 ;
}
public static void main(String[] args)
{
for ( int i = 2 ; i < 100 ; i++)
if (isPower(i))
System.out.print(i + " " );
}
}
|
Python3
import math
def isPower(n) :
if (n = = 1 ) :
return True
for x in range ( 2 ,( int )(math.sqrt(n)) + 1 ) :
y = 2
p = ( int )(math. pow (x, y))
while (p< = n and p> 0 ) :
if (p = = n) :
return True
y = y + 1
p = math. pow (x, y)
return False
for i in range ( 2 , 100 ) :
if (isPower(i)) :
print (i,end = " " )
|
C#
using System;
class GFG
{
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 ;
}
static public void Main ()
{
for ( int i = 2; i < 100; i++)
if (isPower(i))
Console.Write(i + " " );
}
}
|
PHP
<?php
function isPower( $n )
{
if ( $n == 1) return true;
for ( $x = 2; $x <= sqrt( $n ); $x ++)
{
$y = 2;
$p = pow( $x , $y );
while ( $p <= $n && $p > 0)
{
if ( $p == $n )
return true;
$y ++;
$p = pow( $x , $y );
}
}
return false;
}
for ( $i = 2; $i < 100; $i ++)
if (isPower( $i ))
echo $i , " " ;
?>
|
Javascript
<script>
function isPower(n)
{
for (x = 2; x <= Math.sqrt(n); x++) {
var y = 2;
var p = Math.pow(x, y);
while (p <= n && p > 0) {
if (p == n)
return true ;
y++;
p = Math.pow(x, y);
}
}
return false ;
}
for (i = 2; i < 100; i++)
if (isPower(i))
document.write(i + " " );
</script>
|
Output :
4 8 9 16 25 27 32 36 49 64 81
Time Complexity: O(100 * n*log n) //as inbuilt sqrt function takes log n time to execute
Auxiliary Space: O(1)
One optimization in the above solution is to avoid the call to pow() by multiplying p with x one by one.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPower(unsigned int n)
{
if (n <= 1) return true ;
for ( int x=2; x<= sqrt (n); x++)
{
unsigned p = x;
while (p <= n)
{
p *= x;
if (p == n)
return true ;
}
}
return false ;
}
int main()
{
for ( int i =2; i<100; i++)
if (isPower(i))
cout << i << " " ;
return 0;
}
|
Java
class GFG {
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 ;
}
public static void main(String[] args)
{
for ( int i = 2 ; i < 100 ; i++)
if (isPower(i))
System.out.print(i + " " );
}
}
|
Python3
import math
def isPower(n) :
if (n < = 1 ) :
return True
for x in range ( 2 , ( int )(math.sqrt(n)) + 1 ) :
p = x
while (p < = n) :
p = p * x
if (p = = n) :
return True
return False
for i in range ( 2 , 100 ) :
if (isPower(i)) :
print ( i, end = " " )
|
C#
using System;
class GFG
{
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 ;
}
public static void Main()
{
for ( int i = 2; i < 100; i++)
if (isPower(i))
Console.Write(i + " " );
}
}
|
PHP
<?php
function isPower( $n )
{
if ( $n <= 1) return true;
for ( $x = 2; $x <= sqrt( $n ); $x ++)
{
$p = $x ;
while ( $p <= $n )
{
$p *= $x ;
if ( $p == $n )
return true;
}
}
return false;
}
for ( $i = 2; $i < 100; $i ++)
if (isPower( $i ))
echo $i , " " ;
?>
|
Javascript
<script>
function isPower(n)
{
for (let x = 2; x <= parseInt(Math.sqrt(n), 10); x++)
{
let p = x;
while (p <= n)
{
p = p * x;
if (p == n)
return true ;
}
}
return false ;
}
for (let i = 2; i < 100; i++)
if (isPower(i))
document.write(i + " " );
</script>
|
Output: 4 8 9 16 25 27 32 36 49 64 81
Time Complexity: O(100 * n*log(n)), where n is the number ranging from 1 to 100
Auxiliary Space: O(1), as no extra space is required
Alternate Implementation :
C++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
int main()
{
for ( int i = 2; i < 100; i++)
if (isPower(i))
cout << i << " " ;
return 0;
}
|
Java
import java.io.*;
import java.lang.Math;
class GFG {
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 ;
}
public static void main (String[] args)
{
for ( int i = 2 ; i < 100 ; i++)
{
if (isPower(i))
System.out.print(i + " " );
}
}
}
|
Python3
import math
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
for i in range ( 2 , 100 ):
if isPower(i):
print (i, end = " " )
|
C#
using System;
class GFG{
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 ;
}
static public void Main ()
{
for ( int i = 2; i < 100; i++)
{
if (isPower(i))
Console.Write(i + " " );
}
}
}
|
Javascript
<script>
function isPower(n)
{
let p;
if (n <= 1)
{
return true ;
}
for (let i = 2; i <= parseInt(Math.sqrt(n), 10); i++)
{
p = (Math.log(n) / Math.log(i)).toFixed(14);
if ((Math.ceil(p) == Math.floor(p)) && (p > 1))
{
return true ;
}
}
return false ;
}
for (let i = 2; i < 100; i++)
{
if (isPower(i))
document.write(i + " " );
}
</script>
|
Output: 4 8 9 16 25 27 32 36 49 64 81
Time Complexity: O(100 * log n), where n is the number ranging from 1 to 100
Auxiliary Space: O(1), as no extra space is required
Efficient Solution: Check if a number can be expressed as a^b | Set 2
This article is contributed by Vaibhav Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above