Check if N can be converted to the form K power K by the given operation | Python3 implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
kPowKform = dict ( ) NEW_LINE def func ( n ) : NEW_LINE INDENT global kPowKform NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n in kPowKform ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT answer = 0 NEW_LINE x = n NEW_LINE while ( x > 0 ) : NEW_LINE INDENT d = x % 10 NEW_LINE if ( d != 0 ) : NEW_LINE INDENT if ( func ( n - d * d ) ) : NEW_LINE INDENT answer = 1 NEW_LINE break NEW_LINE DEDENT DEDENT x //= 10 NEW_LINE DEDENT return answer NEW_LINE DEDENT def canBeConverted ( n ) : NEW_LINE INDENT if ( func ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 90 NEW_LINE for i in range ( 1 , 9 ) : NEW_LINE INDENT val = 1 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT val *= i NEW_LINE DEDENT kPowKform [ val ] = 1 NEW_LINE DEDENT canBeConverted ( N ) NEW_LINE DEDENT