Find the GCD that lies in given range | Return the greatest common divisor of two numbers ; Return the gretest common divisor of a and b which lie in the given range . ; Loop from 1 to sqrt ( GCD ( a , b ) . ; if i divides the GCD ( a , b ) , then find maximum of three numbers res , i and g / i ; Driven Program
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function maxDivisorRange ( a , b , l , h ) { var g = gcd ( a , b ) ; var res = - 1 ; for ( i = l ; i * i <= g && i <= h ; i ++ ) if ( g % i == 0 ) res = Math . max ( res , Math . max ( i , g / i ) ) ; return res ; } var a = 3 , b = 27 , l = 1 , h = 5 ; document . write ( maxDivisorRange ( a , b , l , h ) ) ;