Count of numbers whose sum of increasing powers of digits is equal to the number itself | Function to return the count of digits of n ; Function to return the sum of increasing powers of N ; To store the required answer ; Count of digits in n which will be the power of the last digit ; While there are digits left ; Get the last digit ; Add the last digit after raising it to the required power ; Decrement the power for the previous digit ; Remove the last digit ; Function to return the count of integers which satisfy the given conditions ; If current element satisfies the given condition ; Driver code
def countDigits ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n //= 10 NEW_LINE DEDENT return cnt NEW_LINE DEDENT def digitPowSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE pw = countDigits ( n ) NEW_LINE while ( n > 0 ) : NEW_LINE INDENT d = n % 10 NEW_LINE sum += pow ( d , pw ) NEW_LINE pw -= 1 NEW_LINE n //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def countNum ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( i == digitPowSum ( i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 200 NEW_LINE print ( countNum ( n ) ) NEW_LINE