Cunningham Numbers | Python3 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 ; Given Number ; Function Call
import math NEW_LINE def isPower ( a ) : NEW_LINE INDENT if ( a == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = 2 NEW_LINE while ( i * i <= a ) : NEW_LINE INDENT val = math . log ( a ) / math . log ( i ) NEW_LINE if ( ( val - int ( val ) ) < 0.00000001 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT def isCunningham ( n ) : NEW_LINE INDENT return isPower ( n - 1 ) or isPower ( n + 1 ) NEW_LINE DEDENT n = 126 NEW_LINE if ( isCunningham ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT