Check if a number can be expressed as x ^ y ( x raised to power y ) | C ++ program to check if a given number can be expressed as power ; Returns true if n can be written as x ^ y ; Driver Program
#include <bits/stdc++.h> NEW_LINE 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 ; }