Program to balance the given Chemical Equation | Function to calculate GCD ; Function to calculate b1 , b2 and b3 ; Variable declaration ; temp variable to store gcd ; Computing GCD ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def balance ( x , y , p , q ) : NEW_LINE INDENT if ( p % x == 0 and q % y == 0 ) : NEW_LINE INDENT b1 = p // x NEW_LINE b2 = q // y NEW_LINE b3 = 1 NEW_LINE DEDENT else : NEW_LINE INDENT p = p * y NEW_LINE q = q * x NEW_LINE b3 = x * y NEW_LINE temp = gcd ( p , gcd ( q , b3 ) ) NEW_LINE b1 = p // temp NEW_LINE b2 = q // temp NEW_LINE b3 = b3 // temp NEW_LINE DEDENT print ( b1 , b2 , b3 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 2 NEW_LINE y = 3 NEW_LINE p = 4 NEW_LINE q = 5 NEW_LINE balance ( x , y , p , q ) NEW_LINE DEDENT