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.

`// Java program to find product of ` `// unique prime factors of a number. ` ` ` `public` `class` `GFG { ` ` ` `public` `static` `long` `productPrimeFactors(` `int` `n) ` ` ` `{ ` ` ` `long` `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 ` ` ` `boolean` `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; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `44` `; ` ` ` `System.out.print(productPrimeFactors(n)); ` ` ` `} ` `} ` ` ` `// Contributed by _omg ` |

*chevron_right*

*filter_none*

**Output:**

22

**Method 2 (Efficient)**

The idea is based on Efficient program to print all prime factors of a given number

`// Java program to find product of ` `// unique prime factors of a number. ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `public` `class` `GFG { ` ` ` `public` `static` `long` `productPrimeFactors(` `int` `n) ` ` ` `{ ` ` ` `long` `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 <= Math.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; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `44` `; ` ` ` `System.out.print(productPrimeFactors(n)); ` ` ` `} ` `} ` ` ` `// Contributed by _omg ` |

*chevron_right*

*filter_none*

**Output:**

22

Please refer complete article on Product of unique prime factors of a number for more details!

## Recommended Posts:

- Java Program for efficiently print all prime factors of a given number
- Java Program to Find sum of even factors of a number
- Java Program to Find minimum sum of factors of number
- Program for Find sum of odd factors of a number
- Java Program for Number of elements with odd factors in given range
- Java Program for Find largest prime factor of a number
- Java program to check if a number is prime or not
- Java Program For Closest Prime Number
- Java Program to Find the Number Occurring Odd Number of Times
- Java Program for Program to find area of a circle
- Java Program to Find Maximum Odd Number in Array Using Stream and Filter
- Java Program to Find Reverse of a Number Using Recursion
- Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop
- Java Program for How to check if a given number is Fibonacci number?
- Java Program to Convert a Decimal Number to Binary & Count the Number of 1s
- Minimum absolute difference of a number and its closest prime
- TCS Coding Practice Question | Checking Prime Number
- Java Program for Program to calculate area of a Tetrahedron
- Java Program for Program for array rotation
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!