Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNumbers ( int a [ ] , int n , int x ) { bool 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 ; cout << a [ i ] << " ▁ " ; } } if ( ! flag ) cout << " There ▁ are ▁ no ▁ such ▁ numbers " ; } int main ( ) { int x = 60 ; int a [ ] = { 2 , 5 , 10 , 7 , 17 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printNumbers ( a , n , x ) ; return 0 ; }