Cunningham Numbers | Java implementation for the above approach ; Function to check if a number can be expressed as a ^ b . ; Function to check if N is a Cunningham number ; Driver Code ; Given Number ; Function Call
import java . util . * ; class GFG { static boolean isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } static boolean isCunningham ( int n ) { return isPower ( n - 1 ) || isPower ( n + 1 ) ; } public static void main ( String [ ] args ) { int n = 126 ; if ( isCunningham ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }