Find LCM of rational numbers | Python 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
import math NEW_LINE def LCM ( a , b ) : NEW_LINE INDENT return ( a * b ) // ( math . gcd ( a , b ) ) NEW_LINE DEDENT def lcmOfNumerator ( vect ) : NEW_LINE INDENT lcm = vect [ 0 ] [ 0 ] NEW_LINE for i in range ( 1 , len ( vect ) ) : NEW_LINE INDENT lcm = LCM ( vect [ i ] [ 0 ] , lcm ) NEW_LINE DEDENT return lcm NEW_LINE DEDENT def gcdOfDemoninators ( vect ) : NEW_LINE INDENT gcd = vect [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , len ( vect ) ) : NEW_LINE INDENT gcd = math . gcd ( vect [ i ] [ 1 ] , gcd ) NEW_LINE DEDENT return gcd NEW_LINE DEDENT def lcmOfRationals ( vect ) : NEW_LINE INDENT print ( lcmOfNumerator ( vect ) , " / " , gcdOfDemoninators ( vect ) , sep = " " ) NEW_LINE DEDENT vect = [ ] NEW_LINE vect . append ( ( 2 , 7 ) ) NEW_LINE vect . append ( ( 3 , 14 ) ) NEW_LINE vect . append ( ( 5 , 3 ) ) NEW_LINE lcmOfRationals ( vect ) NEW_LINE