Sum of given N fractions in reduced form | Function to find GCD of a & b using Euclid Lemma ; Base Case ; Function to find the LCM of all elements in arr [ ] ; Initialize result ; Iterate arr [ ] to find LCM ; Return the final LCM ; Function to find the sum of N fraction in reduced form ; To store the sum of all final numerators ; Find the LCM of all denominator ; Find the sum of all N numerators & denominators ; Add each fraction one by one ; Find GCD of final numerator and denominator ; Convert into reduced form by dividing from GCD ; Print the final fraction ; Given N ; Given Numerator ; Given Denominator ; Function call
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 findlcm ( arr , n ) : NEW_LINE INDENT ans = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans = ( ( ( arr [ i ] * ans ) ) // ( gcd ( arr [ i ] , ans ) ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def addReduce ( n , num , den ) : NEW_LINE INDENT final_numerator = 0 NEW_LINE final_denominator = findlcm ( den , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT final_numerator = ( final_numerator + ( num [ i ] ) * ( final_denominator // den [ i ] ) ) NEW_LINE DEDENT GCD = gcd ( final_numerator , final_denominator ) NEW_LINE final_numerator //= GCD NEW_LINE final_denominator //= GCD NEW_LINE print ( final_numerator , " / " , final_denominator ) NEW_LINE DEDENT N = 3 NEW_LINE arr1 = [ 1 , 2 , 5 ] NEW_LINE arr2 = [ 2 , 1 , 6 ] NEW_LINE addReduce ( N , arr1 , arr2 ) NEW_LINE