Find LCM of rational numbers | JAVASCRIPT program to find LCM of given array ; get lcm of two numbers ; Finds LCM of numerators ; calculate the lcm of all numerators ; return all numerator lcm ; Get GCD of all the denominators ; calculate the gcd of all the denominators ; return all denominator gcd ; find lcm of all the rational number ; return the LCM of all numerator / GCD of all denominator ; Driver code ; give rational number 2 / 7 , 3 / 14 , 5 / 3 make pair as a numerator and denominator
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function LCM ( a , b ) { return Math . floor ( ( a * b ) / ( __gcd ( a , b ) ) ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function lcmOfNumerator ( vect ) { let lcm = vect [ 0 ] . first ; for ( let i = 1 ; i < vect . length ; i ++ ) lcm = LCM ( vect [ i ] . first , lcm ) ; return lcm ; } function gcdOfDemoninators ( vect ) { let gcd = vect [ 0 ] . second ; for ( let i = 1 ; i < vect . length ; i ++ ) gcd = __gcd ( vect [ i ] . second , gcd ) ; return gcd ; } function lcmOfRationals ( vect ) { document . write ( lcmOfNumerator ( vect ) + " " + gcdOfDemoninators ( vect ) ) ; } let vect = [ ] ; vect . push ( new pair ( 2 , 7 ) ) ; vect . push ( new pair ( 3 , 14 ) ) ; vect . push ( new pair ( 5 , 3 ) ) ; lcmOfRationals ( vect ) ;