Check if N can be represented as sum of distinct powers of 3 | Function to check whether the given N can be represented as the sum of the distinct powers of 3 ; Iterate until N is non - zero ; Termination Condition ; Right shift ternary bits by 1 for the next digit ; If N can be expressed as the sum of perfect powers of 3 ; Driver Code
def DistinctPowersOf3 ( N ) : NEW_LINE INDENT while ( N > 0 ) : NEW_LINE INDENT if ( N % 3 == 2 ) : NEW_LINE INDENT cout << " No " NEW_LINE return NEW_LINE DEDENT N //= 3 NEW_LINE DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 91 NEW_LINE DistinctPowersOf3 ( N ) NEW_LINE DEDENT