Find the ratio of LCM to GCD of a given Array | Python3 program to implement above approach ; Function to calculate and return GCD of the given array ; Initialise GCD ; Once GCD is 1 , it will always be 1 with all other elements ; Return GCD ; Function to calculate and return LCM of the given array ; Initialise LCM ; LCM of two numbers is evaluated as [ ( a * b ) / gcd ( a , b ) ] ; Return LCM ; Function to print the ratio of LCM to GCD of the given array ; Driver Code
import math NEW_LINE def findGCD ( arr , n ) : NEW_LINE INDENT gcd = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT gcd = int ( math . gcd ( arr [ i ] , gcd ) ) NEW_LINE if ( gcd == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return gcd NEW_LINE DEDENT def findLCM ( arr , n ) : NEW_LINE INDENT lcm = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT lcm = int ( ( ( ( arr [ i ] * lcm ) ) / ( math . gcd ( arr [ i ] , lcm ) ) ) ) NEW_LINE DEDENT return lcm NEW_LINE DEDENT def findRatio ( arr , n ) : NEW_LINE INDENT gcd = findGCD ( arr , n ) NEW_LINE lcm = findLCM ( arr , n ) NEW_LINE print ( int ( lcm / gcd ) , " : " , "1" ) NEW_LINE DEDENT arr = [ 6 , 12 , 36 ] NEW_LINE N = len ( arr ) NEW_LINE findRatio ( arr , N ) NEW_LINE