Find the ratio of LCM to GCD of a given Array | 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 prvar the ratio of LCM to GCD of the given array ; Driver Code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function findGCD ( arr , n ) { var gcd = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { gcd = __gcd ( arr [ i ] , gcd ) ; if ( gcd == 1 ) { return 1 ; } } return gcd ; } function findLCM ( arr , n ) { var lcm = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) { lcm = ( ( ( arr [ i ] * lcm ) ) / ( __gcd ( arr [ i ] , lcm ) ) ) ; } return lcm ; } function findRatio ( arr , n ) { var gcd = findGCD ( arr , n ) ; var lcm = findLCM ( arr , n ) ; document . write ( ( lcm / gcd ) ) ; document . write ( " " ) ; } var arr = [ 6 , 12 , 36 ] ; var N = 3 ; findRatio ( arr , N ) ;