Find numbers which are multiples of first array and factors of second array | Function to return the LCM of two numbers ; Function to print the required numbers ; To store the lcm of array a [ ] elements and the gcd of array b [ ] elements ; Finding LCM of first array ; Finding GCD of second array ; No such element exists ; All the multiples of lcmA which are less than or equal to gcdB and evenly divide gcdB will satisfy the conditions ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function lcm ( x , y ) { var temp = ( x * y ) / __gcd ( x , y ) ; return temp ; } function findNumbers ( a , n , b , m ) { var lcmA = 1 , gcdB = 0 ; for ( var i = 0 ; i < n ; i ++ ) lcmA = lcm ( lcmA , a [ i ] ) ; for ( var i = 0 ; i < m ; i ++ ) gcdB = __gcd ( gcdB , b [ i ] ) ; if ( gcdB % lcmA != 0 ) { document . write ( " " ) ; return ; } var num = lcmA ; while ( num <= gcdB ) { if ( gcdB % num == 0 ) document . write ( num + " " ) ; num += lcmA ; } } var a = [ 1 , 2 , 2 , 4 ] ; var b = [ 16 , 32 , 64 ] ; var n = a . length ; var m = b . length ; findNumbers ( a , n , b , m ) ;