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
function gcd ( a , b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } function findlcm ( arr , n ) { var ans = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } function addReduce ( n , num , den ) { var final_numerator = 0 ; var final_denominator = findlcm ( den , n ) ; for ( var i = 0 ; i < n ; i ++ ) { final_numerator = final_numerator + ( num [ i ] ) * parseInt ( final_denominator / den [ i ] ) ; } var GCD = gcd ( final_numerator , final_denominator ) ; final_numerator = parseInt ( final_numerator / GCD ) ; final_denominator = parseInt ( final_denominator / GCD ) ; document . write ( final_numerator + " " + final_denominator + " " ) ; } var N = 3 ; var arr1 = [ 1 , 2 , 5 ] ; var arr2 = [ 2 , 1 , 6 ] ; addReduce ( N , arr1 , arr2 ) ;