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