Check if a number can be expressed as x ^ y ( x raised to power y ) | Java program to check if a given number can be expressed as power ; Returns true if n can be written as x ^ y ; Driver Code
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 + " ▁ " ) ; } } }