# Print all numbers whose set of prime factors is a subset of the set of the prime factors of X

• Last Updated : 25 Apr, 2022

Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X.
Examples:

Input: X = 60, a[] = {2, 5, 10, 7, 17}
Output: 2 5 10
Set of prime factors of 60: {2, 3, 5}
Set of prime factors of 2: {2}
Set of prime factors of 5: {5}
Set of prime factors of 10: {2, 5}
Set of prime factors of 7: {7}
Set of prime factors of 17: {17}
Hence only 2, 5 and 10’s set of prime factors is a subset of set of prime
factors of 60.
Input: X = 15, a[] = {2, 8}
Output: There are no such numbers

Approach: Iterate for every element in the array, and keep dividing the number by the gcd of the number and X till gcd becomes 1 for the number and X. If at the end the number becomes 1 after continuous division, then print that number.
Below is the implementation of the above approach:

## C++

 `// C++ program to implement` `// the above approach` `#include ` `using` `namespace` `std;`   `// Function to print all the numbers` `void` `printNumbers(``int` `a[], ``int` `n, ``int` `x)` `{`   `    ``bool` `flag = ``false``;`   `    ``// Iterate for every element in the array` `    ``for` `(``int` `i = 0; i < n; i++) {`   `        ``int` `num = a[i];`   `        ``// Find the gcd` `        ``int` `g = __gcd(num, x);`   `        ``// Iterate till gcd is 1` `        ``// of number and x` `        ``while` `(g != 1) {`   `            ``// Divide the number by gcd` `            ``num /= g;`   `            ``// Find the new gcdg` `            ``g = __gcd(num, x);` `        ``}`   `        ``// If the number is 1 at the end` `        ``// then print the number` `        ``if` `(num == 1) {` `            ``flag = ``true``;` `            ``cout << a[i] << ``" "``;` `        ``}` `    ``}`   `    ``// If no numbers have been there` `    ``if` `(!flag)` `        ``cout << ``"There are no such numbers"``;` `}`   `// Drivers code` `int` `main()` `{` `    ``int` `x = 60;` `    ``int` `a[] = { 2, 5, 10, 7, 17 };` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a);`   `    ``printNumbers(a, n, x);` `    ``return` `0;` `}`

## Java

 `// Java program to implement` `// the above approach` `class` `GFG` `{`   `// Function to print all the numbers` `static` `void` `printNumbers(``int` `a[], ``int` `n, ``int` `x)` `{`   `    ``boolean` `flag = ``false``;`   `    ``// Iterate for every element in the array` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{`   `        ``int` `num = a[i];`   `        ``// Find the gcd` `        ``int` `g = __gcd(num, x);`   `        ``// Iterate till gcd is 1` `        ``// of number and x` `        ``while` `(g != ``1``)` `        ``{`   `            ``// Divide the number by gcd` `            ``num /= g;`   `            ``// Find the new gcdg` `            ``g = __gcd(num, x);` `        ``}`   `        ``// If the number is 1 at the end` `        ``// then print the number` `        ``if` `(num == ``1``) ` `        ``{` `            ``flag = ``true``;` `            ``System.out.print(a[i] + ``" "``);` `        ``}` `    ``}`   `    ``// If no numbers have been there` `    ``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); ` `    `  `}`   `// Drivers code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `x = ``60``;` `    ``int` `a[] = { ``2``, ``5``, ``10``, ``7``, ``17` `};` `    ``int` `n = a.length;`   `    ``printNumbers(a, n, x);` `}` `}`   `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python3 program to implement ` `# the above approach ` `from` `math ``import` `gcd`   `# Function to print all the numbers ` `def` `printNumbers(a, n, x) : `   `    ``flag ``=` `False`   `    ``# Iterate for every element in the array ` `    ``for` `i ``in` `range``(n) :`   `        ``num ``=` `a[i]`   `        ``# Find the gcd ` `        ``g ``=` `gcd(num, x)`   `        ``# Iterate till gcd is 1 ` `        ``# of number and x ` `        ``while` `(g !``=` `1``) : `   `            ``# Divide the number by gcd ` `            ``num ``/``/``=` `g`   `            ``# Find the new gcdg ` `            ``g ``=` `gcd(num, x)`   `        ``# If the number is 1 at the end ` `        ``# then print the number ` `        ``if` `(num ``=``=` `1``) :` `            ``flag ``=` `True``; ` `            ``print``(a[i], end ``=` `" "``);`   `    ``# If no numbers have been there ` `    ``if` `(``not` `flag) :` `        ``print``(``"There are no such numbers"``) `   `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: `   `    ``x ``=` `60` `    ``a ``=` `[ ``2``, ``5``, ``10``, ``7``, ``17` `]` `    ``n ``=` `len``(a) `   `    ``printNumbers(a, n, x)` `    `  `# This code is contributed by Ryuga`

## C#

 `// C# program to implement` `// the above approach` `using` `System;`   `class` `GFG` `{`   `// Function to print all the numbers` `static` `void` `printNumbers(``int` `[]a, ``int` `n, ``int` `x)` `{`   `    ``bool` `flag = ``false``;`   `    ``// Iterate for every element in the array` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{`   `        ``int` `num = a[i];`   `        ``// Find the gcd` `        ``int` `g = __gcd(num, x);`   `        ``// Iterate till gcd is 1` `        ``// of number and x` `        ``while` `(g != 1)` `        ``{`   `            ``// Divide the number by gcd` `            ``num /= g;`   `            ``// Find the new gcdg` `            ``g = __gcd(num, x);` `        ``}`   `        ``// If the number is 1 at the end` `        ``// then print the number` `        ``if` `(num == 1) ` `        ``{` `            ``flag = ``true``;` `            ``Console.Write(a[i] + ``" "``);` `        ``}` `    ``}`   `    ``// If no numbers have been there` `    ``if` `(!flag)` `        ``Console.WriteLine(``"There are no such numbers"``);` `}`   `static` `int` `__gcd(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == 0) ` `        ``return` `a; ` `    ``return` `__gcd(b, a % b); ` `    `  `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `x = 60;` `    ``int` `[]a = { 2, 5, 10, 7, 17 };` `    ``int` `n = a.Length;`   `    ``printNumbers(a, n, x);` `}` `}`   `// This code has been contributed by 29AjayKumar`

## PHP

 ``

## Javascript

 ``

Output:

`2 5 10`

Time Complexity: O(n logn), where n is the time to traverse a given array size and logn operations for gcd function going inside it
Auxiliary Space: O(1), no extra space required

My Personal Notes arrow_drop_up
Recommended Articles
Page :