HCF of array of fractions ( or rational numbers ) | Python 3 program to find HCF of array of ; find hcf of numerator series ; return hcf of numerator ; find lcm of denominator series ; ans contains LCM of arr [ 0 ] [ 1 ] , . . arr [ i ] [ 1 ] ; return lcm of denominator ; Core Function ; found hcf of numerator ; found lcm of denominator ; return result ; Driver Code ; Initialize the every row with size 2 ( 1 for numerator and 2 for denominator ) ; function for calculate the result ; print the result
from math import gcd NEW_LINE def findHcf ( arr , size ) : NEW_LINE INDENT ans = arr [ 0 ] [ 0 ] NEW_LINE for i in range ( 1 , size , 1 ) : NEW_LINE INDENT ans = gcd ( ans , arr [ i ] [ 0 ] ) NEW_LINE DEDENT return ( ans ) NEW_LINE DEDENT def findLcm ( arr , size ) : NEW_LINE INDENT ans = arr [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , size , 1 ) : NEW_LINE INDENT ans = int ( ( ( ( arr [ i ] [ 1 ] * ans ) ) / ( gcd ( arr [ i ] [ 1 ] , ans ) ) ) ) NEW_LINE DEDENT return ( ans ) NEW_LINE DEDENT def hcfOfFraction ( arr , size ) : NEW_LINE INDENT hcf_of_num = findHcf ( arr , size ) NEW_LINE lcm_of_deno = findLcm ( arr , size ) NEW_LINE result = [ 0 for i in range ( 2 ) ] NEW_LINE result [ 0 ] = hcf_of_num NEW_LINE result [ 1 ] = lcm_of_deno NEW_LINE i = int ( result [ 0 ] / 2 ) NEW_LINE while ( i > 1 ) : NEW_LINE INDENT if ( ( result [ 1 ] % i == 0 ) and ( result [ 0 ] % i == 0 ) ) : NEW_LINE INDENT result [ 1 ] = int ( result [ 1 ] / i ) NEW_LINE result [ 0 ] = ( result [ 0 ] / i ) NEW_LINE DEDENT DEDENT return ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT size = 4 NEW_LINE arr = [ 0 for i in range ( size ) ] NEW_LINE for i in range ( size ) : NEW_LINE INDENT arr [ i ] = [ 0 for i in range ( 2 ) ] NEW_LINE DEDENT arr [ 0 ] [ 0 ] = 9 NEW_LINE arr [ 0 ] [ 1 ] = 10 NEW_LINE arr [ 1 ] [ 0 ] = 12 NEW_LINE arr [ 1 ] [ 1 ] = 25 NEW_LINE arr [ 2 ] [ 0 ] = 18 NEW_LINE arr [ 2 ] [ 1 ] = 35 NEW_LINE arr [ 3 ] [ 0 ] = 21 NEW_LINE arr [ 3 ] [ 1 ] = 40 NEW_LINE result = hcfOfFraction ( arr , size ) NEW_LINE print ( result [ 0 ] , " , " , result [ 1 ] ) NEW_LINE DEDENT