Check if a number N can be expressed in base B | Function to check if a number N can be expressed in base B ; Check if n is greater than 0 ; Initialize a boolean variable ; Check if digit is 0 or 1 ; Subtract the appropriate power of B from it and increment higher digit ; Driver code ; Given number N and base B ; Function call
def check ( n , w ) : NEW_LINE INDENT a = [ 0 for i in range ( 105 ) ] ; NEW_LINE p = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT a [ p ] = n % w NEW_LINE p += 1 NEW_LINE n //= w NEW_LINE DEDENT flag = True NEW_LINE for i in range ( 101 ) : NEW_LINE INDENT if ( a [ i ] == 0 or a [ i ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( a [ i ] == w or a [ i ] == w - 1 ) : NEW_LINE INDENT a [ i + 1 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT flag = False NEW_LINE DEDENT DEDENT return flag NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT B = 3 NEW_LINE N = 7 NEW_LINE if ( check ( N , B ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT