Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | Java program to implement the above approach ; 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
class GFG { static void printNumbers ( int a [ ] , int n , int x ) { boolean flag = false ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; int g = __gcd ( num , x ) ; while ( g != 1 ) { num /= g ; g = __gcd ( num , x ) ; } if ( num == 1 ) { flag = true ; System . out . print ( a [ i ] + " ▁ " ) ; } } if ( ! flag ) System . out . println ( " There ▁ are ▁ no ▁ such ▁ numbers " ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int x = 60 ; int a [ ] = { 2 , 5 , 10 , 7 , 17 } ; int n = a . length ; printNumbers ( a , n , x ) ; } }