C/C++ Program to find Product of unique prime factors of a number Last Updated : 05 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a number n, we need to find the product of all of its unique prime factors. Prime factors: It is basically a factor of the number that is a prime number itself. Examples: Input: num = 10 Output: Product is 10 Explanation: Here, the input number is 10 having only 2 prime factors and they are 5 and 2. And hence their product is 10. Input : num = 25 Output: Product is 5 Explanation: Here, for the input to be 25 we have only one unique prime factor i.e 5. And hence the required product is 5. Method 1 (Simple) Using a loop from i = 2 to n and check if i is a factor of n then check if i is prime number itself if yes then store product in product variable and continue this process till i = n. CPP // C++ program to find product of // unique prime factors of a number #include <bits/stdc++.h> using namespace std; long long int productPrimeFactors(int n) { long long int product = 1; for (int i = 2; i <= n; i++) { // Checking if 'i' is factor of num if (n % i == 0) { // Checking if 'i' is a Prime number bool isPrime = true; for (int j = 2; j <= i / 2; j++) { if (i % j == 0) { isPrime = false; break; } } // condition if 'i' is Prime number // as well as factor of num if (isPrime) { product = product * i; } } } return product; } // driver function int main() { int n = 44; cout << productPrimeFactors(n); return 0; } Output: 22 Method 2 (Efficient) The idea is based on Efficient program to print all prime factors of a given number CPP // C++ program to find product of // unique prime factors of a number #include <bits/stdc++.h> using namespace std; // A function to print all prime factors of // a given number n long long int productPrimeFactors(int n) { long long int product = 1; // Handle prime factor 2 explicitly so that // can optimally handle other prime factors. if (n % 2 == 0) { product *= 2; while (n % 2 == 0) n = n / 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= sqrt(n); i = i + 2) { // While i divides n, print i and // divide n if (n % i == 0) { product = product * i; while (n % i == 0) n = n / i; } } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) product = product * n; return product; } // driver function int main() { int n = 44; cout << productPrimeFactors(n); return 0; } Output: 22 Please refer complete article on Product of unique prime factors of a number for more details! Comment More infoAdvertise with us Next Article C++ Program to find sum of even factors of a number K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads C++ Program to find sum of even factors of a number Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, ⦠pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respect 3 min read C++ Program To Find All Factors of A Natural Number Given a natural number n, print all distinct divisors of it. Examples: Input : n = 10 Output: 1 2 5 10 Input: n = 100 Output: 1 2 4 5 10 20 25 50 100 Input: n = 125 Output: 1 5 25 125 Note that this problem is different from finding all prime factors. Recommended PracticeCount Numbers in RangeTry It 3 min read C++ Program to Find Factorial of a Number Using Iteration Factorial of a number n is the product of all integers from 1 to n. In this article, we will learn how to find the factorial of a number using iteration in C++. Example Input: 5Output: Factorial of 5 is 120Factorial of Number Using Iteration in C++To find the factorial of a given number we can use l 2 min read Product of unique prime factors of a number Given a number n, we need to find the product of all of its unique prime factors. Prime factors: It is basically a factor of the number that is a prime number itself. Examples : Input: num = 10 Output: Product is 10 Explanation: Here, the input number is 10 having only 2 prime factors and they are 5 11 min read Product of unique prime factors of a number Given a number n, we need to find the product of all of its unique prime factors. Prime factors: It is basically a factor of the number that is a prime number itself. Examples : Input: num = 10 Output: Product is 10 Explanation: Here, the input number is 10 having only 2 prime factors and they are 5 11 min read Distinct Prime Factors of Array Product Given an array of integers. Let us say P is the product of elements of the array. Find the number of distinct prime factors of product P. Examples: Input : 1 2 3 4 5 Output : 3 Explanation: Here P = 1 * 2 * 3 * 4 * 5 = 120. Distinct prime divisors of 120 are 2, 3 and 5. So, the output is 3. Input : 7 min read Like