Cunningham Numbers | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = log ( a ) / log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } bool isCunningham ( int n ) { return isPower ( n - 1 ) || isPower ( n + 1 ) ; } int main ( ) { int n = 126 ; if ( isCunningham ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }