Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Drivers code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function printNumbers ( a , n , x ) { let flag = false ; for ( let i = 0 ; i < n ; i ++ ) { let num = a [ i ] ; let g = __gcd ( num , x ) ; while ( g != 1 ) { num = parseInt ( num / g ) ; g = __gcd ( num , x ) ; } if ( num == 1 ) { flag = true ; document . write ( a [ i ] + " " ) ; } } if ( ! flag ) document . write ( " " ) ; } let x = 60 ; let a = [ 2 , 5 , 10 , 7 , 17 ] ; let n = a . length ; printNumbers ( a , n , x ) ;