
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++
In this problem, we are given a set of N numbers and a number X. And we have to print all numbers from the array whose set of prime factors is a subset of the set of prime factors of X.
Let’s take an example to understand the problem
Input: X= 30 , array = {2, 3, 6, 10, 12} Output : 2 3 6
To solve this problem, we have to traverse elements of the array. And divide this element with gcd of (element, x). Repeat division till the gcd becomes 1. And print the remaining number.
Example
#include <bits/stdc++.h> using namespace std; void printPrimeSet(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]); cout<<"Numbers whose set of prime numbers is subset of set of prime factor of "<<x<<"\n"; printPrimeSet(a, n, x); return 0; }
Output
Numbers whose set of prime numbers is a subset of the set of prime factor of 60
2 5 10
- Related Articles
- Find all prime factors of a number - JavaScript
- C Program for efficiently print all prime factors of a given number?
- Write the prime factors of 18.
- Print all prime factors and their powers in C++
- Count common prime factors of two numbers in C++
- Python Program for Efficient program to print all prime factors of a given number
- Prime factors of a big number in C++
- Find all the prime factors of 1729 and arrange them in ascending order. Now state the relation, if any; between two consecutive prime factors.
- Prime factors of LCM of array elements in C++
- Express 625 as exponents of prime factors.
- Maximum number of unique prime factors in C++
- Prime factors in java
- Product of unique prime factors of a number in Python Program
- Express 429 as a product of its prime factors.
- Express the following as a product of prime factors.$108times192$

Advertisements